Javascript / Discord.js Accessing variables from other files

583
April 19, 2017, at 4:07 PM

while using Html with Js I can load my files and variables in the global scope are accessible everywhere. But what to do, when there is no HTML, just plain JS?

I have a file called bot.js with this code:

// other stuff..
client.login(token); // bot token

Next to this file I have a folder with a file in it called commonResources.js with this code

// other stuff..
var token = "myToken";

The application says, token is not defined. How can I access it?

Answer 1

Considering this files are back-end files (nodeJS) this is the correct way to do it. At the bottom of the file that has the variable you require you will type this command to execute the second file:

require('./bot.js')(token);

After that you will go to the file that you need to use token and type this at the top:

module.exports = function (token) {
//use your token
}
READ ALSO
How to create an extension in QlikView for a dendrogram

How to create an extension in QlikView for a dendrogram

I am new to Qlikview extensions and I have a build an extension for a dendrogram within quick timeHowever, I am unable to understand how to proceed

467
What is the correct way to specify an object's class with the JSDoc 3 syntax?

What is the correct way to specify an object's class with the JSDoc 3 syntax?

If my code creates a pre-ecmascript 6 style "class", ie

291
Process images with WebAssembly

Process images with WebAssembly

I'm searching for example of processing images with WebAssembly in browser or nodejsIt could be pure wast or compiled with emscripten from another language

325
How to disable “BIU” buttons at onscreen keyboard (ipad safari)

How to disable “BIU” buttons at onscreen keyboard (ipad safari)

My page contains text editor based onSafari`s keyboard contain buttons "BIU" (bold, italic, underline)

250