11

I have the following script:

/* script.js */
var bar = "bar1";

function foo() {
    console.log('this.bar: ' + this.bar);
    console.log('global.bar: ' + global.bar);
}
foo();

Running node script.js returns:

this.bar: undefined
global.bar: undefined

However, from inside the node command line environment, reproducing the same script returns:

this.bar: bar1
global.bar: bar1

Further, if I change my variable declaration from var bar = "bar1"; to global.bar = "bar1"; both methods of running the above code return:

this.bar: bar1
global.bar: bar1

What's the difference? Is global variable assignment somehow different when running a script vs reproducing a script in the same environment?

monners
  • 4,784
  • 2
  • 22
  • 44
  • 1
    Running a script that way is basically running a module, and a module is just basically your code spliced into a function before being `eval`'d and invoked, so the `var` is a local variable in that function. The REPL runs in the global execution context. – cookie monster Aug 16 '14 at 04:13
  • possible duplicate of [In what object does Node.js store variables?](http://stackoverflow.com/a/25122846/1048572) – Bergi Aug 16 '14 at 10:55

2 Answers2

5

Simply because every node module is wrapped in an IIFE of sorts, so by default you are not in the global scope.

We can see it happening in src/node.js, in the NativeModule function.

NativeModule.require = function(id) {
    // ...
    var nativeModule = new NativeModule(id);

    nativeModule.cache();
    nativeModule.compile();

    return nativeModule.exports;
};

Following the trail, we look into compile:

NativeModule.prototype.compile = function() {
    var source = NativeModule.getSource(this.id);
    source = NativeModule.wrap(source);

    var fn = runInThisContext(source, { filename: this.filename });
    fn(this.exports, NativeModule.require, this, this.filename);

    this.loaded = true;
};

wrap looks relevant, let's see what it does:

NativeModule.wrap = function(script) {
    return NativeModule.wrapper[0] + script + NativeModule.wrapper[1];
};

NativeModule.wrapper = [
    '(function (exports, require, module, __filename, __dirname) { ',
    '\n});'
];

As suspected, it wraps your module's code in an IIFE, meaning you do not run in the global scope.

On the other hand, the REPL by default runs in the global scope. Following the REPL code is meh, but it basically boils down to this line:

if (self.useGlobal) {
    result = script.runInThisContext({ displayErrors: false });
} else {
    result = script.runInContext(context, { displayErrors: false });
}

Which looks darn relevant to me.

Zirak
  • 35,198
  • 12
  • 75
  • 89
3

http://nodejs.org/api/globals.html

var something inside a Node module will be local to that module.

I think that when you run something from file, it is interpreted as module for node. And if you execute in command shell, variable on top of scope become global.

monkeyinsight
  • 4,024
  • 1
  • 16
  • 26