Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global variable assignment in node from script vs command line

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?

like image 565
monners Avatar asked Aug 16 '14 03:08

monners


2 Answers

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.

like image 157
Zirak Avatar answered Oct 15 '22 13:10

Zirak


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.

like image 26
monkeyinsight Avatar answered Oct 15 '22 12:10

monkeyinsight