You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
bash-3.2$ node ~/tmp/test.js
events.js:235
console.error('(node) warning: possible EventEmitter memory ' +
^
TypeError: console.error is not a function
at process.addListener (events.js:235:17)
at process.stderr (node.js:652:17)
at console.js:92:53
at NativeModule.compile (node.js:954:5)
at Function.NativeModule.require (node.js:902:18)
at node.js:200:27
at MyEmitter.addListener (events.js:235:9)
at Object.<anonymous> (/Users/james/tmp/test.js:10:11)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
bash-3.2$
So, node sets up the global objects e.g. console lazily to make startup really fast. The second call to myEmitter.on('event', ()=>{}); in your test.js wants to trigger console.error with that big message about max listeners detected. So it has to initialize the global console.
The way these initializations work is they store an empty object as the global object, then eval the relevant file which fills in the properties of this object when it modifies module.exports. This is to prevent an infinite loop if you have a circular dependency.
But console.js does module.exports = new Console(process.stdout, process.stderr);, which references process.stdout and process.stderr, causing a call to the stdout and stderr getters on lines 660 and 675 of src/node.js, which both call
if ((stdout / stderr).isTTY) {
process.on('SIGWINCH', function() {
. The second one of these wants to trigger another warning about max listeners detected, so it has to initialize the global console. But there's already such an initialization in progress, which has cached an empty object in the global console, so this empty object is returned as the global console. An empty object doesn't have an error method, so you get this stack trace.
There's no problem in the REPL because stdout and stderr aren't TTY, so these SIGWINCH events aren't added to process.
Activity
jasnell commentedon Dec 29, 2015
Same error does not occur using the repl:
vkurchatkin commentedon Dec 29, 2015
Reproduces with:
console.errortriggers some events. Documentation states:So this seems kind of expected.
events: make sure console functions exist
davidvgalbraith commentedon Dec 30, 2015
So, node sets up the global objects e.g.
consolelazily to make startup really fast. The second call tomyEmitter.on('event', ()=>{});in yourtest.jswants to triggerconsole.errorwith that big message about max listeners detected. So it has to initialize the global console.The way these initializations work is they store an empty object as the global object, then
evalthe relevant file which fills in the properties of this object when it modifiesmodule.exports. This is to prevent an infinite loop if you have a circular dependency.But
console.jsdoesmodule.exports = new Console(process.stdout, process.stderr);, which referencesprocess.stdoutandprocess.stderr, causing a call to thestdoutandstderrgetters on lines 660 and 675 ofsrc/node.js, which both call. The second one of these wants to trigger another warning about max listeners detected, so it has to initialize the global
console. But there's already such an initialization in progress, which has cached an empty object in the global console, so this empty object is returned as the global console. An empty object doesn't have anerrormethod, so you get this stack trace.There's no problem in the REPL because
stdoutandstderraren't TTY, so theseSIGWINCHevents aren't added toprocess.I propose a fix at #4479.
events: make sure console functions exist
events: make sure console functions exist
events: make sure console functions exist