← Back to Node.js, page 2

nodejs-0032 · Medium mid

What does the MaxListenersExceededWarning mean, and how do you find and fix an EventEmitter listener leak?

Node warns when more than 10 listeners are added for one event on one emitter, because that is usually a leak: code adds a listener on every request or call and never removes it. Fix the cause by adding once or removing listeners when done. Raise the limit only if many listeners are intended.

Full answer

Every listener keeps its function, and everything the function captures, alive for as long as the emitter lives. If a long-lived emitter (a database connection, process, a shared event bus) receives a new listener on every request, memory grows without end and every emit calls more and more functions.

Node helps by printing MaxListenersExceededWarning when more than defaultMaxListeners (10) listeners are added to one event name. The message shows the event name and the count. The warning is a hint, not the limit: the extra listeners are still added.

How to find the leak: read the event name in the warning, then search for the on() call. Run with node --trace-warnings to see where the listener was added. Check emitter.listenerCount(name) over time.

How to fix:

  • Use once when the listener should run a single time.
  • Pair each on with an off in clean-up code (a finally block, a close handler, a component unmount).
  • Add listeners once at start-up, not inside a request handler.
  • Use AbortSignal with events.once or events.on so waiting can be cancelled.
  • If the count is deliberately large (a server with many subscribers), raise it with emitter.setMaxListeners(n), or 0 for unlimited, after being sure it is intended.
javascript
const { EventEmitter } = require('node:events');

process.on('warning', (w) => console.log('warning event:', w.name, '| emitter event:', w.type, '| count:', w.count));

const bus = new EventEmitter();

// Leaky pattern: a new listener on every request, never removed.
function handleRequestLeaky() {
  bus.on('config-changed', () => {});
}
for (let i = 0; i < 11; i++) handleRequestLeaky();
console.log('listeners after 11 leaky requests:', bus.listenerCount('config-changed'));

// Fixed pattern: remove the listener when the request is finished.
const fixedBus = new EventEmitter();
function handleRequestFixed() {
  const onChange = () => {};
  fixedBus.on('config-changed', onChange);
  // ... request work ...
  fixedBus.off('config-changed', onChange); // clean-up
}
for (let i = 0; i < 11; i++) handleRequestFixed();
console.log('listeners after 11 fixed requests:', fixedBus.listenerCount('config-changed'));

Node also prints the warning text to stderr, so you will see it in the terminal as well as in the 'warning' event output. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): listeners after 11 leaky requests: 11 listeners after 11 fixed requests: 0 warning event: MaxListenersExceededWarning | emitter event: config-changed | count: 11

Key points

  • More than 10 listeners per event triggers a warning.
  • The warning signals a probable leak, not a hard limit.
  • Fix with once, off, one-time registration or AbortSignal.
  • setMaxListeners is for intentional large counts.

Common mistakes

  • Silencing the warning with setMaxListeners(0) without finding the cause.
  • Adding listeners inside request handlers on a shared emitter.
  • Using an anonymous function and then being unable to remove it.
  • Ignoring the warning because the app still works.

Follow-ups

  • How would you debug a leak when no warning is shown?
  • What does node --trace-warnings print?
  • How can closures in listeners keep large objects alive?

Tips

  • Explain the leak with one picture: a growing list of functions inside a long-lived object.
  • Name the three fixes: once, off, register once.
  • Mention --trace-warnings as the debugging tool.