MaxListenersExceededWarning: What It Means and How to Fix It
Error reference
MaxListenersExceededWarning
Language
Severity
LowWhen it happens
Node.js emits this warning when more than 10 listeners (the default maximum) are added to a single event on an EventEmitter.
Potential fixes
Remove listeners when they are no longer needed with .off() or .removeListener(). Use .once() for one-shot listeners. If you genuinely need more than 10 listeners, call emitter.setMaxListeners(n). Treat the warning as a signal to review your listener lifecycle.
Deep dive
Node.js emits this warning when more than 10 listeners (the default maximum) are added to a single event on an EventEmitter. It is a warning, not an error, designed to help detect potential memory leaks from forgotten listener removal.
Why it happens
- •Listeners are added in a loop or on every request without being removed.
- •A function that attaches a listener is called multiple times without cleanup.
- •Third-party libraries attach listeners to shared emitters without removing them.
Potential fixes
Remove listeners when they are no longer needed with .off() or .removeListener(). Use .once() for one-shot listeners. If you genuinely need more than 10 listeners, call emitter.setMaxListeners(n). Treat the warning as a signal to review your listener lifecycle.
Examples
Code that triggers the error
const EventEmitter = require("events");
const emitter = new EventEmitter();
for (let i = 0; i < 20; i++) {
emitter.on("data", () => console.log("received"));
}Error output
(node:12345) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 data listeners added to [EventEmitter]. Use emitter.setMaxListeners() to increase limit
Fixed code
const EventEmitter = require("events");
const emitter = new EventEmitter();
function handleData() {
console.log("received");
}
emitter.on("data", handleData);
// When done:
emitter.off("data", handleData);Practice in English
How would you explain a MaxListenersExceededWarning to a fellow dev? Choose the right phrase:
"I hit an error...
Ready to practice your English at work?
Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.
Try Lingua-e for free