0

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
3
  • 1
    Just add var _ = require("./underscore.js"); to your bar.js aswell. Commented Oct 23, 2012 at 22:11
  • @Mahn thanks, yes and that's what Ivan Vergiliev said as well. Problem solved, and now adding a requirement that I need to use _ in both "main.js" and "bar.js" I am considering which solution is better: Vyacheslav Voronchuk's (passing in _), or yours (require in two places). Commented Oct 25, 2012 at 18:08
  • I'd just include the requires in each module; it makes sense when you think of the content of each module as a separate entity. Commented Oct 25, 2012 at 18:13

2 Answers 2

3

Yes, you should use in every module which needs that variable, for the case of you example.

var _ = require("./underscore.js");

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.

process._ = require("./underscore.js");

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.

module.exports = function(_) {
   var e = {};
   var array = [...];
   e.publish = function(t, args) {
      _.each(array, function(...) {...});
   });
   return e;
}

In main.js:

var _ = require("./underscore.js");
var foo = require("./bar.js")(_);
foo.publish(...);

I hope you got the point.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the solution, I tested both Ivan and Mahn solution and yours and they all work well, Your is perhaps best because I am planning to use _ in main.js and bar.js
2

The variable to which you assign the result is local to the module main.js, so you can't access it in bar.js. Instead, you should require the underscore module in bar.js as well.

You could also skip the var when declaring _ and make it a global variable, but that brings all the problems of global variables, like:

  • the bar.js module is not explicit about its dependencies, so it's harder to understand that it expects underscore to be required globally;
  • it requires a specific initialization order - if you later move the _ = require(underscore), you'll get undefined errors again, and it might be hard to understand why;
  • every other module that requires bar.js needs to also require underscore.

1 Comment

Thanks, this way totally works (i.e. call require within a module) and it seems like a best practice if the lib is used only within the top level module. I am considering using the passing in solution (from Vyacheslav), mainly because I also require underscore in other parts of main.js...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.