Advertisement
Advanced Node.js, Events & EventEmitter

What is the default maximum listener limit on an EventEmitter, and why does Node.js warn when it's exceeded?

Short answer

By default, an EventEmitter instance warns (via a MaxListenersExceededWarning) if more than 10 listeners are added for the same event name -- this default limit exists specifically as a heuristic to help catch memory leaks, since accidentally adding a new listener repeatedly…

Save Node.js mock interview
Advertisement
AModel answer
By default, an EventEmitter instance warns (via a MaxListenersExceededWarning) if more than 10 listeners are added for the same event name -- this default limit exists specifically as a heuristic to help catch memory leaks, since accidentally adding a new listener repeatedly (like inside a loop or a request handler, without ever removing old ones) is a very common source of unbounded memory growth; the limit can be raised via setMaxListeners() when a legitimately larger number of listeners is expected and intentional.
const emitter = new EventEmitter();
emitter.setMaxListeners(20); // deliberately raising the limit for a case with many legitimate listeners

// Without raising it, adding an 11th listener triggers a warning:
// (node:12345) MaxListenersExceededWarning: Possible EventEmitter memory leak detected.
Real-world example
A team investigating a MaxListenersExceededWarning in their logs discovers it was correctly flagging a real bug -- a route handler was calling .on() on a shared EventEmitter singleton on every request without ever removing the listener, exactly the kind of leak the default warning is designed to surface.
Likely follow-ups
  • Should raising the max listener limit ever be the actual fix, versus investigating whether it's masking a real leak?
  • How would you programmatically inspect exactly how many listeners are attached to diagnose a suspected leak?
Related topics