'this' different between REPL and script
Node's REPL
is global. Code from a file is in a "module", which is really just a function.
Your code file turns into something like this very simplified example:
var ctx = {};(function(exports) { // your code console.log(this === global);}).call(ctx, ctx);
Notice that it's executed using .call()
, and the this
value is set to a pre-defined object.
When you use node to run script from a file, it implicitly sets it up as a module with its own scope.
When you just run it without a file, you're dropped into the REPL but not in any module scope.