Node.js Reference ยท Events and Timers
Node.js setMaxListeners() and the listener limit
Reference for emitter.setMaxListeners(), getMaxListeners() and events.defaultMaxListeners in Node.js: the leak warning, how to catch it and when to raise it.
By default an emitter warns when more than 10 listeners are registered for the same event. The warning is a heuristic for a common bug: code that calls on() every time a request, a retry or a render happens and never removes the listener, so memory and CPU grow without bound.
setMaxListeners(n) changes the threshold for one emitter, getMaxListeners() reads it, and EventEmitter.defaultMaxListeners changes the default for all emitters created afterward. The limit is a warning only: nothing is blocked and the listener is still added.
emitter.setMaxListeners(20);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
emitter.setMaxListeners(n: number): this emitter.getMaxListeners(): number EventEmitter.defaultMaxListeners: number // 10 events.setMaxListeners(n?: number, ...eventTargets: (EventEmitter | EventTarget)[]): void events.getMaxListeners(emitterOrTarget): number
| Name | Description |
|---|---|
n |
A non-negative number. 0 or Infinity means unlimited. |
eventTargets |
Emitters or EventTarget objects to configure. With none, the call changes defaultMaxListeners. |
Returns: setMaxListeners() returns the emitter; getMaxListeners() returns the current limit.
Errors: ERR_OUT_OF_RANGE if n is negative or not a number.
Usage notes
- A warning, not an error. The text printed to stderr is
MaxListenersExceededWarning: Possible EventEmitter memory leak detected, followed by the count, event name and the advice to raise the limit. The program continues. - Once per event. Each event name warns one time per emitter, no matter how many more listeners are added.
- Catch it.
process.on('warning', ...)receives aMaxListenersExceededWarningwithemitter,typeandcountproperties, which is handy for logging or tests. The flag--trace-warningsshows where the extra listener was added. - Fix the cause first. Raising the limit hides a leak. Legitimate cases exist: a hub with one listener per connected client, or many
AbortSignalconsumers sharing a signal. - Signals and targets.
EventTargetobjects, includingAbortSignal, have the same check. Useevents.setMaxListeners(n, signal)to configure them. - Default changes are global.
EventEmitter.defaultMaxListenersapplies to every emitter in the process that has not calledsetMaxListeners()itself, existing ones included, so every library sees it. Prefer per-emitter settings. getMaxListeners(target)fromnode:eventsalso works onEventTarget, which has no method of its own.- Streams and sockets are emitters, so piping many streams into one destination triggers the same warning.