Node.js EventEmitter Events Reference

Core Node.js built-in event names by module with their payload arguments.

Reference for Node.js built-in EventEmitter events across http, net, stream, process and child_process — each event name, the module that emits it, its payload arguments and when it fires. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

What is special about the error event?

If an EventEmitter emits error with no listener attached, Node treats it as an unhandled exception and crashes the process by throwing. Always attach an error listener to streams, sockets and servers before they can fail, even if you only log.

Node’s core APIs are event-driven

Most Node.js I/O objects extend EventEmitter and signal progress and completion by emitting named events. Knowing each event name, which object emits it, and the listener’s arguments is essential to wiring up servers, streams and processes. This reference lists the core built-in events by module.

How it works

You react to an event by registering a listener with the documented signature. The emitter calls every listener synchronously, in order, with the event’s payload arguments:

server.on("request", (req, res) => res.end("ok"));
readable.on("data", (chunk) => total += chunk.length);
readable.on("end", () => console.log("done"));
readable.on("error", (err) => console.error(err)); // always attach
child.on("exit", (code, signal) => console.log(code, signal));

Use on for repeated events and once for one-shot events. The error event is special: an unhandled error throws and crashes the process.

Key events by module

http.Server

  • request — emitted for each incoming request; listener receives (req: IncomingMessage, res: ServerResponse). The main handler for HTTP servers.
  • connection — a new TCP socket has been established, before any request is parsed.
  • close — the server has stopped accepting connections (after server.close() drains existing connections).

Readable streams (fs.createReadStream, http.IncomingMessage, etc.)

  • data — a chunk is available; emitted only in flowing mode. Listener receives (chunk: Buffer | string).
  • end — no more data will be provided; the read side is finished.
  • error — an error occurred reading; always handle this or the process may crash.
  • close — the underlying file descriptor or socket has been released.

Writable streams (fs.createWriteStream, http.ServerResponse, etc.)

  • drain — the internal buffer has been flushed and the stream can accept more writes; important when write() returns false.
  • finish — all data has been flushed to the underlying system after calling end().
  • error — write error occurred.

net.Socket

  • connect — connection established.
  • data — data received from the remote end.
  • end — the other end has signalled it has no more data to send (half-close).
  • close — socket is fully closed; receives (hadError: boolean).

child_process.ChildProcess

  • spawn — child process has started successfully.
  • exit — child process terminated; listener receives (code: number | null, signal: NodeJS.Signals | null).
  • close — both exit has fired AND all stdio streams have closed. Use this when you need to know all output has been captured.
  • error — the process could not be spawned, killed, or sent a message.

process

  • exit — process is about to exit; listener receives (code: number). Only synchronous operations here.
  • uncaughtException — an exception was not caught; log and exit cleanly.
  • unhandledRejection — a Promise rejection was not handled; listener receives (reason, promise).
  • SIGINT / SIGTERM — signals for graceful shutdown (Ctrl+C, container stop).

Tips and notes

  • Always attach an error listener to every stream, socket and server before use.
  • data is emitted only in flowing mode — calling .on("data") switches a readable stream into flowing mode.
  • For child processes, wait for close (not just exit) when you must be sure all stdout/stderr has been flushed.
  • Removing listeners with removeListener/off prevents leaks on long-lived emitters; watch for the MaxListenersExceededWarning.