Node.js "MaxListenersExceededWarning" - Fix EventEmitter Leaks in CI
Node warns when an EventEmitter accumulates more than 10 listeners for one event - usually a sign that listeners are added in a loop or per request without ever being removed.
What this error means
A noisy MaxListenersExceededWarning appears in logs (and a stack trace if --trace-warnings is on). In long test runs it can precede slow growth in memory. The warning itself does not crash, but it points at a real leak.
(node:1) MaxListenersExceededWarning: Possible EventEmitter memory leak
detected. 11 SIGTERM listeners added to [process]. Use emitter.setMaxListeners()
to increase limitCommon causes
Listeners added without removal
A function called repeatedly (per request, per test, in a loop) attaches a listener each time but never calls removeListener/off. The count climbs past the default limit of 10.
A genuinely high but bounded listener count
Some legitimate designs attach more than 10 listeners to one emitter on purpose. Here the warning is a false positive and the limit should be raised intentionally.
How to fix it
Find and remove the leaked listeners
Run with --trace-warnings to get the stack where the listener is added, then ensure each on() has a matching off() (or use once()).
node --trace-warnings app.mjs
# in code: remove what you add
const onTerm = () => shutdown();
process.on('SIGTERM', onTerm);
// later
process.off('SIGTERM', onTerm);Raise the limit only when the count is legitimate
If many listeners are correct by design, raise the threshold on that specific emitter - not globally as a way to silence a real leak.
emitter.setMaxListeners(50);How to prevent it
- Pair every
on()with anoff(), or useonce()for one-shot handlers. - Run with
--trace-warningsin CI so the leak source is visible. - Only call
setMaxListenerswhen a high count is intentional and bounded.