Cluster
cluster.fork()
Creates a worker from the primary. Keep worker counts bounded and supervise unexpected exits with backoff rather than unconditional rapid restarts. Tested on Node.js 22.16.0. Example output (one line per entry): ["catalog"].
Formal Syntax & Structure
nodejs syntaxnodejs
cluster.fork([env])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| env | object | Additional environment variables. |
Return Value
Worker
Cluster worker handle.
Working Example
Example Snippetnodejs
import cluster from 'node:cluster';import { once } from 'node:events';if (cluster.isPrimary) { const w = cluster.fork({ LAB_LABEL: 'catalog' }); const done = once(w, 'exit'); console.log((await once(w, 'message'))[0]); await done;}else { process.send(process.env.LAB_LABEL, () => process.disconnect());}