peterHasemann
peterHasemann

Reputation: 1580

Accessing variables from other files

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?

Upvotes: 0

Views: 2080

Answers (1)

itsundefined
itsundefined

Reputation: 1450

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
}

Upvotes: 1

Related Questions