Cluster

cluster.worker.id

Identifies a cluster worker within its primary. It is not a tenant identifier or persistent identity across restarts. Tested on Node.js 22.16.0. Example output (one line per entry): ["true"].

Formal Syntax & Structure

nodejs syntaxnodejs
cluster.worker.id

Return Value

number

Worker ID.

Working Example

Example Snippetnodejs
import cluster from 'node:cluster';import { once } from 'node:events';if (cluster.isPrimary) {    const w = cluster.fork({});    const done = once(w, 'exit');    console.log((await once(w, 'message'))[0] === w.id);    await done;}else {    process.send(cluster.worker.id, () => process.disconnect());}
Node.js Reference