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 mand 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?
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 mand 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?
Share Improve this question asked Aug 16, 2014 at 3:49 monnersmonners 5,3022 gold badges30 silver badges47 bronze badges 2-
2
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 thevar
is a local variable in that function. The REPL runs in the global execution context. – cookie monster Commented Aug 16, 2014 at 4:13 - possible duplicate of In what object does Node.js store variables? – Bergi Commented Aug 16, 2014 at 10:55
2 Answers
Reset to default 5Simply 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.pile();
return nativeModule.exports;
};
Following the trail, we look into pile
:
NativeModule.prototype.pile = 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/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 mand shell, variable on top of scope bee global.