Tuesday, 21 July 2020

Question about require( ) case-sensitivity and weird bug

I will preface by saying that I am fairly new to JS, node, SQL, and server stuff as a whole so this might be a stupid question. For context, I am making a discord bot with an online dashboard. I am working in windows 10.I have a really weird interaction between some of the modules I am trying to export and require into other files and I am hoping someone can shed some light on what is happening.So I have the module in a file named "DataBus.js" that connects to a MySQL database and handles getting and saving data for the app. I included this module in 3 places: my app entry file called "bot.js", a command processing file called "CommandManager.js", and the endpoint for my web dashboard called "index.js". index.js is not a module export. After requiring it in bot.js I then ran a function to connect the SQL database.In bot.js I required CommandManager.js to pass received discord messages to be processed, and index.js to run the web dashboard. The catch here is when I used require to add the DataBus.js module to each of those files I had written the file name in all lower case (databus.js). For some reason, this worked just fine when I now wish it had not.Fast forward to today, I am trying to set the bot up on a server using Ubuntu and this mistake crippled my entire app. Ubuntu did not like DataBus.js being required in lowercase. I figured it was an easy enough mistake to fix and just changed it to the correct casing. Now it seems that variables that I once was able to change and share between all 3 files are now undefined. For instance, if I now try to grab the database object from DataBus.js in CommandManager.js it returns as undefined. The even stranger part is if I change the require in CommandManager.js back to lowercase it works again.​A copy/paste of the important parts////////// bot.js ////////// ... const DataBus = require('./bot_modules/DataBus.js'); const CommandManager = require('./bot_modules/CommandManager.js'); require('./dash/endpoint/index.js');DataBus.Connect(); CommandManager.PopulateModuleLibrary(); ...////////// CommandManager.js ////////// ... //Test function will return with object const DataBus = require("./databus.js"); //Test function will return undefined const DataBus = require("./DataBus.js"); ... module.exports = { Test: function(){ console.log(DataBus.database) } ... } ////////// DataBus.js ////////// module.exports = { database: mysql.createConnection({ host: "localhost", user: "root", password: process.env.SQL_PASSWORD, database: process.env.SQL_DATABASE }), //Run in bot.js Connect: function() { this.database.connect(err => { if (err) throw err; console.log("Connected to database!"); }); }, } From what I understand, when you require a module, it gets cached and the same instance will get returned if you try to require it elsewhere. So therefore if I make changes to a module in one file, it will update in the other files. And as far as I know, that is true since it did work like that originally.I really have no clue what is going on here, but as I think about it the more I fear that I will have to restructure all my code if I actually just didn't understand how modules work.

Submitted July 22, 2020 at 03:56AM by Nilloc_Kcirtap

No comments:

Post a Comment