Description
- Version: v8.1.2
- Platform: Darwin bayswater.local 16.6.0 Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; root:xnu-3789.60.24~6/RELEASE_X86_64 x86_64
- Subsystem: repl
I think the built-in repl module's default option value of useGlobal: false
is likely very surprising for many use cases, and should be reconsidered. For reference, the node
CLI sets this value manually to true
, so the REPL that Node users are accustomed to has their code run in a single shared context.
What I just discovered today, while working on testdouble.js with its little repl script is that the consequences of useGlobal
defaulting to false are quite striking. We found a very insiduous bug that manifested itself as functions defined in the REPL not seeming to have Object
or Function
in their prototype chain (which, of course, every function
does).
After lots of digging, we discovered it was due to the fact that useGlobal
is false by default. For a minimal example of how absurd this seems, see the following output:
$ node -e 'require("repl").start()'
> setTimeout instanceof Function
false
Of course, it's ridiculous that setTimeout
, or any built-in/host method would fail an instanceof
check with Function
. With the node
CLI or the useGlobal
option manually set to true
, however, things behave much more akin to a real-world Node.js program.
$ node -e 'require("repl").start({useGlobal: true})'
> setTimeout instanceof Function
true
$ node
> setTimeout instanceof Function
true
As a result, it seems to me that useGlobal: true
would have been a more sensible default for the built-in repl module, but since it's stable, maybe we can at least document the ramifications of this quirk in behavior. Thoughts?