'Global' object in node.js

As of NodeJS v0.8.14 global seems to work across modules like the window object does in the browser.

Test:

a.js:

a1 = console.log;  // Will be accessed from b.js
global.a2 = console.log;  // Will be accessed from b.js

require('./b.js');

b1('a: b1');
b2('a: b2');
global.b1('a: global.b1');
global.b2('a: global.b2');

b.js:

a1('b: a1');
a2('b: a2');
global.a1('b: global.a1');
global.a2('b: global.a2');

b1 = console.log;  // Will be accessed from a.js
global.b2 = console.log;  // Will be accessed from a.js

Running a.js outputs:

b: a1
b: a2
b: global.a1
b: global.a2
a: b1
a: b2
a: global.b1
a: global.b2

Apparently, the global object isn't the global object as window is in the browser. It's (according to micheil in #nodejs @ freenode) really only used internally. Something about global closures and whatnot.

parseInt and setTimeout and all those buddies are globals on their own. Not part of any visible global object.


Defining variable in app.js without var, just like myvar='someval' makes it visible inside every .js in your project