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?

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 the var is a local variable in that function. The REPL runs in the global execution context. Commented Aug 16, 2014 at 4:13
  • possible duplicate of In what object does Node.js store variables? Commented Aug 16, 2014 at 10:55

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.