Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Module

node:events

Node.js's node:events module provides EventEmitter for named, synchronous notifications. Register persistent or one-time listeners, emit events with arguments, and remove listeners when they are no longer needed.

How it works

The node:events module provides EventEmitter, whose instances associate named events with listener functions. Register listeners with on() or once(), trigger them with emit(), and remove them with off() or removeListener(); listeners run synchronously in registration order.

Syntax

import { EventEmitter } from 'node:events';
const emitter = new EventEmitter();
emitter.on(eventName, listener);
emitter.once(eventName, listener);
emitter.emit(eventName, ...args);

Parameters

NameTypeDescription
eventNamestring | symbolEvent name or event key identifying the notification channel.
listener(...args: any[]) => voidFunction invoked when the event is emitted.
argsany[]Values forwarded to listeners by emit().

Returns

EventEmitter methods such as on(), once(), off(), and removeListener() return the emitter for chaining; emit() returns a boolean indicating whether listeners were called.

Key points

  • Import EventEmitter from node:events to create event-publishing objects.
  • Use on() for registration and emit() to publish a named event.
  • Listeners run immediately and preserve registration order.
  • A listener added with on() remains active across repeated emissions.
  • once() is suitable for one-shot notifications.
  • Pass event-specific data as arguments to emit().

Common mistakes

  • Event handlers execute immediately during emit(), so a slow listener can delay subsequent work.

Example: Register and emit events

import { EventEmitter } from "node:events";

const bus = new EventEmitter();

bus.once("ready", (name) => {
  console.log(`${name} is ready`);
});

bus.on("message", (text) => {
  console.log(text);
});

bus.emit("ready", "worker");
bus.emit("ready", "worker");
bus.emit("message", "processed");

Creates an emitter, registers a one-time listener and a regular listener, then emits events with arguments.

When to use it

Application event bus

Coordinate internal application components without tightly coupling them to each other.

Lifecycle notifications

Notify consumers about lifecycle transitions such as startup, shutdown, or connection changes.

Core API integration

React to stream, server, or resource events exposed by Node.js APIs.

Interview questions

QWhat is EventEmitter used for?

EventEmitter is the class exposed by node:events for objects that publish named events. on() attaches listeners, and emit() invokes listeners for the selected event.

Follow-up: How are listeners scheduled? They run synchronously, in the order they were registered, unless a listener explicitly defers its own work.

QHow do on() and once() differ?

on() registers a listener that remains attached for every matching emission. once() removes its listener after the first matching event, so it handles at most one occurrence.

Follow-up: When would you choose once()? Use it for one-time lifecycle events such as initialization or a first connection.

QWhat happens when emit() is called?

emit() invokes listeners for the named event and can pass arguments to them. Listener return values are not used by EventEmitter.

Follow-up: How should failures be handled? Attach an error listener because an emitted error without a registered error handler is special and can terminate the process.

  • EventEmitter
  • EventEmitterAsyncResource
  • EventEmitter.on
  • EventEmitter.once
  • EventEmitter.emit
  • EventEmitter.off
  • EventEmitter.removeListener
  • events.once

Source: Events. Checked 2026-10-15.