Node.js global variables

Harry picture Harry · Mar 27, 2011 · Viewed 259.9k times · Source

I asked here: Does Node.js require inheritance?

And I was told that I can set variables to the global scope by leaving out the variable.

This does not work for me.

That is, the following does not make the _ available on required files.

_ = require('underscore');

I can set with Express.js's app.set and have it available elsewhere though.

Is that how this is supposed to work?

Answer

masylum picture masylum · Mar 27, 2011

You can use global like so:

global._ = require('underscore')