9

I am using 0.3.1-pre Node.js

Doing this:

typeof global.parseInt

results in

'undefined'

However when pressing [Tab] in the console after typing 'global.' gives a list of functions, including parseInt.

So is parseInt a member of the global namespace or not?

Art
  • 23,747
  • 29
  • 89
  • 101

4 Answers4

14

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
zupa
  • 12,809
  • 5
  • 40
  • 39
11

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.

Tor Valamo
  • 33,261
  • 11
  • 73
  • 81
  • Does it mean that decorating the global object does not really make it accessible out of the module, or that it would be accessible but only it as a property of the global object in other modules? – Radagast the Brown Jan 11 '12 at 09:52
  • 3
    Whatever you do to the global object is nullified at any time. Whenever the internal node process does something that requires use of it, it resets. So it's not reliable (at all) for any persistant globals. In fact, the only place I've seen it work for more than half a second is in the node-repl (command line). – Tor Valamo Jan 11 '12 at 10:14
  • 1
    thanks for the comment. that is so sad :(, especially when many people in many places direct you to the global object – Radagast the Brown Jan 11 '12 at 13:57
  • 2
    I see they updated the documentation for `global` (there was always a bit of confusion as to what it actually did). It now does contain the "global" variables you set (outside any function scope), but there is a new global object for every module, so it is not cross module. `var a = 1` in index.js will be available as `global.a` only in index.js. – Tor Valamo Jan 11 '12 at 20:11
9

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

2pietjuh2
  • 879
  • 2
  • 13
  • 29
bFunc
  • 1,370
  • 1
  • 12
  • 22
  • 1
    Is that a direct answer to the question? I don't think so. – itsbruce Nov 22 '12 at 12:18
  • 2
    It is an answer to the possible intentions behind the question or intentions of others landing here. Like any answer should be. Thanks bFunc. – zupa Feb 07 '13 at 16:22
0

FAILS:

if( global[ some_object_i_want_to_exist ] ){ ... }

WORKS:

//: outside of all functions, including IIFE.
const THE_GLOBAL_YOU_PROBABLY_WANT_IS_THIS=( this );

//: Within a function:
const G = THE_GLOBAL_YOU_PROBABLY_WANT_IS_THIS;
if( G[ some_object_i_want_to_exist ] ){ ... }

I am assuming you got to this page about "global" in node.js because you wanted the equivalent of "window" in order to check for globally declared variables. bFunc's solution didn't work for me, as it seems to require that one explicitly does something like:

global.some_object_i_want_to_exist = whatever;

as a pre-requisit to using

global[ some_object_i_want_to_exist ]

EDIT: Looking at my code it seems that the only reason my solution worked is because I used "exports.some_object_i_want_to_exist" somewhere else in the file. Without that, my solution fails. So... I have no clue how to reliable determine if an object exists in a given scope in Node.js.

Here is the documentation on global object: https://nodejs.org/api/globals.html

I am going to leave my answer here because I hear people are more likely to correct you when you are wrong, so maybe someone will correct me with the answer to the problem.

KANJICODER
  • 3,611
  • 30
  • 17