What I want
Is it possible to pass locals to a required module?
For example:
// in main.js
var words = { a: 'hello', b:'world'};
require('module.js', words);
// in module.js
console.log(words.a + ' ' + words.b) // --> Hello World
I’m asking this because in PHP when you require or include, the file which includes another files inherits it’s variables, which is very useful in some cases, and I would be happy if this could be done in node.js too.
What I have tried and didn’t worked
var words = { a: 'hello', b:'world'};
require('module.js', words);
var words = { a: 'hello', b:'world'};
require('module.js');
Both of these gives ReferenceError: words is not defined when words is called in module.js
So is it possible at all without global variables?
What you want to do is export it with an argument so you can pass it the variable.
module.jsmain.jsYou can also chop off the .js in the require 🙂