Search code examples
javascriptnode.jsrequire

Does Node.js require inheritance?


In my server.js file I included the Underscore.js library.

var _ = require('underscore')

I have my routes like this:

// require routes
require('./routes/document');

In the document route, I want to use Underscore.js. But it seems like the _ variable is not inherited/inside the document scope. Does that mean I have to set the _ variable on every single required route? Or is there a more intelligent way to do this?


Solution

  • Yes, you should set the _ in the files that needs it to be available.

    Alternatively, you can put it in the global scope by removing the var part.

    _ = require('underscore');
    require('./routes/document'); // _ will be visible in document as well