I want to use one module feature within another module
file main.js
var _ = require("./underscore.js");
var foo = require("./bar.js");
foo.publish(...);
file bar.js
(function(e) {
var array = [...];
e.publish = function(t, args) {
_.each(array, function(...) {...});
});
})(exports);
I’ve tried a couple of variations, but I am not sure of the best way around this error:
ReferenceError: _ is not defined
Yes, you should use in every module which needs that variable, for the case of you example.
But if you need to transfer one object instance between several modules you can use process object to make it global. But that is BAD practice.
Good way to pass object instances between the modules is to pass them as function arguments, but you need to change you bar.js to return a factory function, not an object itself.
In main.js:
I hope you got the point.