Cluster
cluster.isPrimary
Identifies the primary process in cluster. Cluster workers are separate processes, unlike worker threads. Tested on Node.js 22.16.0. Example output (one line per entry): ["true false"].
Formal Syntax & Structure
nodejs syntaxnodejs
cluster.isPrimaryReturn Value
boolean
Primary-process flag.
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(cluster.isPrimary, (await once(w, 'message'))[0]); await done;}else { process.send(cluster.isPrimary, () => process.disconnect());}