I'm creating a module in node.js and i would like to access to the variables of the level where is being loaded the module.
var externalvar = 12345;
var mymodule = require('mymodule');
Is possible access to externalvar inside of mymodule?
Yes.
var externalvar = 12345;
var mymodule = require('mymodule')(externalvar);
Inside your module:
module.exports = function(val) {
console.log(val); //12345
};
I think this is the best and the only way to do it.
externalvar = 12345;
Or
global.externalvar = 12345;
Inside the module
module.exports = function() {
console.log(global.externalvar); //12345
};
But this is not working
var externalvar = 12345;
console.log(global.externalvar); //undefined