Reputation: 2118
Is there way to define global variable which will be available inside some module. So, if we have module test, and inside test we have files index.js, test1.js, test2.js, can we define variable inside index.js to be available in test1.js and test2.js, but not outside this module?
Exact situation:
So, I am using some npm module and I don't want to require that module every time, but only within one directory, not whole node application, because that can cause problems to me.
Upvotes: 0
Views: 180
Reputation: 475
test1.js:
exports = test1Module = {
func1: function(...){...},
func2: function(...){...},
func3: function(...){ console.log(test1Module.secretVariable); }
}
index.js:
var secretVariable = "secretValue";
var test1 = require('test1');
test1.secretVariable = secretVariable;
And similarly with test2, but NOT test3..
Upvotes: 1
Reputation: 2734
What you are looking for is namespaces.
Take a look at the following How do I declare a namespace in JavaScript?
var yourNamespace = {
foo: function() {
},
bar: function() {
}
};
...
yourNamespace.foo();
Upvotes: 0