How to Fix MaxListenersExceededWarning in Node.js
If you see MaxListenersExceededWarning: Possible EventEmitter memory leak detected. in Node.js, more than 10 listeners are registered for the same event on one EventEmitter. If you keep adding listeners with on() and forget to remove them with off(), this warning appears. Removing the listeners you no longer need with off() makes the warning go away.
This article covers the case where you see "memory leak" in your logs while calling a function that waits for an event many times, or while registering an event on every request in a server. In a test environment, I deliberately triggered this warning, kept the message as it was printed, and confirmed that the warning disappeared after I fixed the code.
The test environment was Ubuntu 24.04 with Node.js 22.23.5. I wrote the code as ES modules (.mjs). I did not test other Node.js versions, CommonJS (require), warnings that come from EventTarget such as AbortSignal, or warnings that come from inside libraries or frameworks.
- 1. Triggering the warning with a listener that was never removed
- 2. Finding where the listener is registered with –trace-warnings
- 3. Removing a listener once it is no longer needed with off()
- 4. Raising the limit with setMaxListeners() when registering many listeners is intentional
- 5. On the same event of the same EventEmitter, the warning appears only once
Triggering the warning with a listener that was never removed
For the test, I used code that calls a function waitForDone, which waits until the job with the given id finishes, 11 times in a row. Each call of waitForDone adds a listener to the done event, but the listener is not removed after the wait ends.
import { EventEmitter } from 'node:events';
const bus = new EventEmitter();
// Function that waits until the job with the specified id finishes (forgets to remove the listener)
function waitForDone(id) {
return new Promise((resolve) => {
bus.on('done', (doneId) => {
if (doneId === id) resolve();
});
});
}
for (let id = 1; id <= 11; id++) {
const waiting = waitForDone(id);
setTimeout(() => bus.emit('done', id), 10);
await waiting;
}
console.log('終わったあとに残っているリスナーの数:', bus.listenerCount('done'));
When I ran node leak.mjs, the output was as follows.
(node:532) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 done listeners added to [EventEmitter]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit
(Use `node --trace-warnings ...` to show where the warning was created)
終わったあとに残っているリスナーの数: 11
The (node:532) at the start is the process ID, which changes with each run. The message means "possible EventEmitter memory leak: 11 listeners were added to done. The limit is 10."
This is a warning, not an error. The process ran to the end and exited with code 0. The Node.js events documentation also says the limit is not a strict cap: you can still add listeners beyond it, and the only effect is a warning on standard error. In the test environment, when I ran the code with standard error discarded (node leak.mjs 2>/dev/null), the warning was not shown and only the last line was printed. If you do not keep standard error in your logs, the problem keeps running without anyone noticing.
Looking at the last line, 11 listeners remain even after all 11 waits have finished. Each listener was never removed after use, so they keep piling up. Each additional call to this function adds another listener.
Finding where the listener is registered with –trace-warnings
The message does not show which file and line registered the listener. As the second line of the output above suggests, if you run with --trace-warnings, the call site at the moment the warning is emitted is printed (the path is replaced with an example value).
(node:539) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 done listeners added to [EventEmitter]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit
at genericNodeError (node:internal/errors:983:15)
at wrappedFn (node:internal/errors:537:14)
at _addListener (node:events:593:17)
at EventEmitter.addListener (node:events:611:10)
at file:///path/to/app/leak.mjs:8:9
at new Promise (<anonymous>)
at waitForDone (file:///path/to/app/leak.mjs:7:10)
at file:///path/to/app/leak.mjs:15:19
Lines whose parentheses start with node: are internal Node.js processing, so you can skip them. The line below them, leak.mjs:8:9, is the bus.on('done', ...) line, and leak.mjs:15:19 is the line that calls waitForDone(id). This shows where the 11th listener was added, so start there and check where that listener is removed.
Removing a listener once it is no longer needed with off()
I changed waitForDone so that when the awaited id arrives, the listener removes itself. To remove a listener, you must pass the same function you registered to off(). So I stored the listener in a variable named onDone before registering it.
// When the awaited id arrives, remove its own listener
function waitForDone(id) {
return new Promise((resolve) => {
const onDone = (doneId) => {
if (doneId !== id) return;
bus.off('done', onDone);
resolve();
};
bus.on('done', onDone);
});
}
The rest of the code is the same as before. When I ran node fixed.mjs, the warning did not appear, and no listeners remained (0). Running it with --trace-warnings gave the same output.
終わったあとに残っているリスナーの数: 0
emitter.off() is an alias of emitter.removeListener(). If you pass an inline function to on(), you cannot pass the same function to off() later, so you cannot remove that specific listener. Give a name to any listener you need to remove before you register it.
When you see this warning, check whether you are removing listeners that are no longer needed before you raise the limit. Raising the limit does not stop listeners that were never removed from piling up.
Raising the limit with setMaxListeners() when registering many listeners is intentional
Some designs have many components receiving one event. For example, 12 UI components might each receive a theme event that announces a theme change. In that case, having 12 listeners is the correct state, but without any change, the warning appears once the count goes past the limit of 10.
12個の部品が dark を受け取りました
上限: 10 リスナーの数: 12
(node:560) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 theme listeners added to [EventEmitter]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit
(Use `node --trace-warnings ...` to show where the warning was created)
As the message suggests, you can change the limit of a specific EventEmitter with emitter.setMaxListeners(). I set it to a value slightly higher than the number of listeners I register.
import { EventEmitter } from 'node:events';
const bus = new EventEmitter();
// 12 UI components each receive the same 'theme' event (intentionally registering many)
bus.setMaxListeners(20);
for (let i = 1; i <= 12; i++) {
bus.on('theme', (name) => {
if (i === 12) console.log(`12個の部品が ${name} を受け取りました`);
});
}
bus.emit('theme', 'dark');
console.log('上限:', bus.getMaxListeners(), 'リスナーの数:', bus.listenerCount('theme'));
When I ran it, the warning no longer appeared.
12個の部品が dark を受け取りました
上限: 20 リスナーの数: 12
According to the documentation, passing Infinity or 0 to setMaxListeners() removes the limit. In the test environment, I set setMaxListeners(0) and registered 30 listeners, and no warning appeared. However, once the limit is removed, the warning can no longer alert you if listeners pile up because you forgot to remove them. If you set the limit slightly above the number you expect, the warning will appear when you forget to remove a listener.
There is also events.defaultMaxListeners, which changes the default limit for all EventEmitters. The documentation warns that it applies to all EventEmitters, including ones created before the change. You only need to raise the limit for EventEmitters that you know will receive many listeners. If you change only that EventEmitter's limit with setMaxListeners(), the warning will still appear for forgotten listeners on other EventEmitters, as before.
On the same event of the same EventEmitter, the warning appears only once
In the test environment, I registered 30 listeners on the done event of the same EventEmitter. The warning appeared only once, for "11 done listeners added," and did not appear again at the 20th or 30th listener.
リスナーの数: 30
(node:574) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 done listeners added to [EventEmitter]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit
(Use `node --trace-warnings ...` to show where the warning was created)
Just because the warning appeared only once does not mean the count stopped at 11. You can check how many listeners are currently registered with emitter.listenerCount('event name'). After you fix the code, repeat the operation a few times, then print this count and confirm that it does not keep growing. That way you can be sure.





