0

I want to define a variable in one file called vars.js. then I want to access those variable from another file called mybot.js. this is what I ahve in each file:

vars.js: var token = 'abcfgk6'

mybot.js:

var request = require(./vars.js);
...
bot.login(token);
1
  • Please always search before posting a question. This has been answered already many many times. Commented Oct 17, 2016 at 14:45

3 Answers 3

2

You need to export your variable in vars.js. See also this StackOverflow thread here for detailed explanations. Your code could look e.g. like this:

// vars.js
exports.token = 'abcfgk6';
// mybot.js
var request = require('./vars.js');
bot.login(token);
Sign up to request clarification or add additional context in comments.

Comments

1

You have to export the variable in vars.js

var token = 'abcfgk6'
exports.token = token;

And then access via:

var request = require(./vars.js);
...
bot.login(request.token);

Hope it helps!

Comments

1

Use json in your external files. It makes it allot easier to manage large amounts of data.

vars.js:

module.exports = {
    'token': 'abcfgk6'
};

Then access it with:

var request = require(./vars.js);

request.token;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.