EventEmitter is the Node.js class, from the node:events module, that lets an object announce named events and lets other code listen for them. You register a listener with on, trigger it with emit, and every listener for that event name runs in the order it was added. This is the nodejs eventemitter pattern behind streams, HTTP servers and many libraries.
Examples target Node.js 22 or 24 LTS and use ES modules.
on(eventName, listener) adds a listener. emit(eventName, ...args) calls every listener with the arguments you pass and returns true if there was at least one listener, otherwise false. The event name can be a string or a symbol.
emit does not queue anything. It calls the listeners one after another before it returns:
A slow listener therefore blocks the code that called emit. If a listener must do async work, start the work and return, and handle its errors inside the listener.
| Method | What it does |
|---|---|
on(name, fn) / addListener | Add a listener |
once(name, fn) | Add a listener that runs one time, then removes itself |
off(name, fn) / removeListener | Remove one listener |
removeAllListeners(name) | Remove all listeners for a name |
prependListener(name, fn) | Add a listener at the front of the list |
listenerCount(name) | Number of listeners for a name |
eventNames() | Names that currently have listeners |
To remove a listener you must keep a reference to the same function, so avoid passing a fresh inline arrow function to off.
Most real code makes its own class inherit from EventEmitter so that instances can emit events about their own state:
Callers get a clear public surface: call start, subscribe to progress and finished. Pass one object as the event payload when you have several values, which keeps listeners readable and lets you add fields later.
For a regular function listener, this refers to the emitter. In an arrow function listener, this is the surrounding scope, not the emitter:
Use an arrow function when you want the surrounding this, and a regular function or the emitter variable when you need the emitter.
events.once returns a promise that resolves with an array of the event arguments, which lets you use await with events:
This function also rejects if the emitter emits error while waiting, so failures are not lost. Node.js also provides events.on(emitter, name), which returns an async iterator over repeated events.
By default an emitter prints a warning when more than 10 listeners are added for one event name. This warning usually means a leak, for example adding a listener on every request and never removing it. Fix the cause by removing listeners with off or using once. If you really need more, raise the limit with emitter.setMaxListeners(n).
No. emit calls listeners synchronously, in registration order. Asynchronous behaviour only appears if a listener schedules work itself.
on keeps the listener for every future event, while once removes it after the first call.
Call emitter.off(name, fn) with the same function reference you passed to on. An inline arrow function cannot be removed because you have no reference to it.
They are similar ideas, but they are different APIs. Node.js also provides EventTarget for code that must match the web API.
EventEmitter gives objects on, once, off and emit for named events with synchronous, ordered listeners. Extend it for your own classes, send one payload object, use events.once to await an event, and watch for the listener-limit warning as a sign of a leak.