With the given file:
console.js
const repl = require('repl');
repl.start('> ');
Then running in nodejs 11 & 12:
$ node console.js
> console.log({} instanceof Object)
false
But running with nodejs 10:
$ node console.js
> console.log({} instanceof Object)
true
I am expecting to be true in all cases, did I miss something?
It's caused by this code:
https://github.com/nodejs/node/blob/5b8df5e956680dc1a38b631e53f5e70a905fd917/lib/repl.js#L877-L880
That copies the Object from the main context to the REPL context.
I'm not completely sure what commit introduced this but cc @BridgeAR since you were the last one to touch that code. :-)
Workaround until it's fixed: repl.start({ prompt: '> ', useGlobal: true })
Hi @bnoordhuis, thank you for the workaround.
With some more experiments, I can reproduce on nodejs 10 the bug with this piece of code:
const repl = require('repl');
function test(obj) {
return obj instanceof Object;
}
repl.start('> ').context.test = test;
$ node console.js
> test({})
false
But with { useGlobal: true } it does work correctly:
const repl = require('repl');
function test(obj) {
return obj instanceof Object;
}
repl.start({prompt: '> ', useGlobal: true}).context.test = test;
$ node console.js
> test({})
true
Maybe this bug is related to https://github.com/nodejs/node/commit/e3055dc5254502d2f02276f20c9a3f37b41e7f12?
cc @BridgeAR.
Closed since it was already fixed via https://github.com/nodejs/node/pull/28561.
Most helpful comment
It's caused by this code:
https://github.com/nodejs/node/blob/5b8df5e956680dc1a38b631e53f5e70a905fd917/lib/repl.js#L877-L880
That copies the
Objectfrom the main context to the REPL context.I'm not completely sure what commit introduced this but cc @BridgeAR since you were the last one to touch that code. :-)
Workaround until it's fixed:
repl.start({ prompt: '> ', useGlobal: true })