Search code examples
javascriptnode.jsscopeglobalread-eval-print-loop

NodeJS REPL: Why does this variable assignment fail?


Can someone help me understand the following behavior? I'd expect that, since I can set the global f from inside this callback, I should also be able to change it. I don't understand well enough how node handles context vs. global in the REPL to make sense of this, and I'd appreciate any insight.

Start a REPL without useGlobal

$ cat test.js 
var repl = require('repl');
repl.start({useGlobal:false});

Now try setting f twice in a row:

$ node test.js 
> f
ReferenceError: f is not defined
>  setTimeout(function(){f=1;}, 0);
> f
1

Works the first time... now try again:

>  setTimeout(function(){f=2;}, 0);
> f
1

Huh!

The first run sets it; the second doesn't affect it.

(Setting useGlobal:true I get the behavior I expect.)


Solution

  • It's because of Node's faulty vim module which is what's running your code behind the scenes. When you disable useGlobal this changes the manner in which it runs your code, changing from vm.runInThisContext to vm.runInContext [1]. Essentially what happens is that it copies all the variables from the specified sandbox object over to the actual global object it runs your code in, executes the code, then copies everything back. When you use setTimeout the change is made after everything is copied back. The blame for this lies partially in the repl module [2] and partially in the vm module [3]. I believe the vm module is slated for an overhaul for the next version.