Skip to content
Advertisement

Reference errors appearing when importing discord bot modules

Ok so I am writing a music bot due to the more populars getting shutdown and the remaining being pretty useless. I split up the code in separate files for better readability and all that and so am using modules to import and export function & variables and when I run the program using node index.js it tells me that I’m getting a reference error and that I need to initialized a variable before use even though it’s already been initialized.

This is my index.js code:

JavaScript

and this is my play.js code:

JavaScript

Lastly, this is the error I get:

JavaScript

I’ve tried to find someone with the same problem online but none of them have had the issue in the same context. What I’ve figured out is that when I import the getCommand function from play.js that’s when it breaks, I’ve tried running both files seperately and they still break if the function is imported into index.js. I would remove the function import from index.js but I can’t since it’s how I plan to call the rest of the functions in the rest of the play.js file.

Advertisement

Answer

From what @ouroborus commented this was circular dependency issue. I solved this by creating a new file name global-vars in which I placed all global variables needed by both of them so that they depend on a third file and not eachother, it also allowed for slightly cleaner code.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement