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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With