Node.js warns about a possible EventEmitter memory leak
MaxListenersExceededWarning: Possible EventEmitter memory leak detectedShort answer
Node.js prints this warning when more than 10 listeners are added to one event on one emitter. It is a warning, not a crash. Run with --trace-warnings to see where the listeners come from. Usually code adds a listener on every request and never removes it. Remove it, use once(), or raise the limit on purpose.
Fix it fast
1Find out which cause you have
Start the app with stack traces for warnings. The trace points at the line that added the extra listener, and the warning names the emitter and the event.
node --trace-warnings server.js2Match what you see, then jump to its fix
- 1
The warning appears after the app has handled a few requests, and the count in the message keeps growing.A listener is added on every call and never removedFix - 2
The warning appears right at start-up, and the listeners are all intended (for example one per worker or per plugin).You really need more than 10 listeners on one eventFix - 3
The warning names an AbortSignal, and emitter.setMaxListeners() or events.defaultMaxListeners did nothing.The emitter is an AbortSignal or another EventTargetFix
- 1
What does the error look like?
MaxListenersExceededWarning: Possible EventEmitter memory leak detectedpossible EventEmitter memory leak detected
Cause 1 of 3
A listener is added on every call and never removed
1Check
Look at the line from --trace-warnings. If it sits inside a request handler, a loop or a retry function, each call adds a fresh listener to a long-lived emitter. You can also count the listeners for one event.
const { getEventListeners } = require('node:events');
console.log(getEventListeners(emitter, 'data').length);2Fix
Register the listener once, outside the handler. If it must live per call, keep a reference to the function and remove it with off() when the work is done. Use once() when only the first event matters.
function onData(chunk) { /* ... */ }
emitter.on('data', onData);
// when finished with this call:
emitter.off('data', onData);
// or, for a one-time reaction:
emitter.once('ready', () => { /* ... */ });3Verify
Run the same traffic again. The count stays flat and the warning does not return.
node --trace-warnings server.jsConfirmed by nodejs.org
Cause 2 of 3
You really need more than 10 listeners on one event
1Check
Read the current limit for that emitter. The default is 10 per event.
const { getMaxListeners } = require('node:events');
console.log(getMaxListeners(emitter));2Fix
Raise the limit on that one emitter. Keep the number close to what you expect, so a real leak still trips the warning. Changing events.defaultMaxListeners affects every emitter, so avoid it.
emitter.setMaxListeners(20);3Verify
Restart the app and confirm the warning is gone.
node --trace-warnings server.jsConfirmed by nodejs.org
Cause 3 of 3
The emitter is an AbortSignal or another EventTarget
1Check
The docs state that defaultMaxListeners has no effect on AbortSignal instances. The same signal is probably passed to many operations that each add an abort listener.
const { getMaxListeners } = require('node:events');
console.log(getMaxListeners(signal));2Fix
Use the events.setMaxListeners() function, which works on EventTarget objects too, or share one signal across fewer operations.
const { setMaxListeners } = require('node:events');
setMaxListeners(50, signal);3Verify
Run the code path that fans out the work and check that no warning is printed.
node --trace-warnings app.jsConfirmed by nodejs.org
How do I stop it coming back?
- Pair every on() inside a function with an off() in the same function's cleanup path.
- Prefer once() for one-shot events such as 'ready' or 'close'.
- Leave the default limit of 10 in place, so a new leak is still reported.
- Run tests with --trace-warnings so a new warning shows its origin.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix