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);
Jwiggiff
  • 95
  • 1
  • 8
  • Please always search before posting a question. This has been answered already many many times. – cviejo Oct 17 '16 at 14:45

3 Answers3

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);
Community
  • 1
  • 1
mxscho
  • 1,990
  • 2
  • 16
  • 27
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!

jos
  • 1,070
  • 12
  • 22
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;
Tom
  • 2,543
  • 3
  • 21
  • 42