Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Node.js warns about a possible EventEmitter memory leak

ErrorMaxListenersExceededWarning: Possible EventEmitter memory leak detected

Short 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.

  • Applies to Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.js
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The 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. 3The warning names an AbortSignal, and emitter.setMaxListeners() or events.defaultMaxListeners did nothing.The emitter is an AbortSignal or another EventTargetFix

What does the error look like?

  • MaxListenersExceededWarning: Possible EventEmitter memory leak detected
  • possible 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.js

Confirmed by nodejs.org

Did this fix it?

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.js

Confirmed by nodejs.org

Did this fix it?

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.js

Confirmed by nodejs.org

Did this fix it?

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.