Child process

child_process.fork()

Launches a separate Node process with an IPC channel, not a shared JavaScript heap. Use a fixed trusted entry module and an explicit message contract. Tested on Node.js 22.16.0. Example output (one line per entry): ["4"].

Formal Syntax & Structure

nodejs syntaxnodejs
fork(modulePath[, args][, options])

Parameters & Attributes

NameTypeDescription
modulePathstring | URLNode entry module.
argsstring[]Arguments.
optionsobjectProcess settings.

Return Value

ChildProcess

Node subprocess with IPC.

Working Example

Example Snippetnodejs
import { writeFileSync, unlinkSync } from 'node:fs';import { fork } from 'node:child_process';import { once } from 'node:events';const file = new URL('./child.cjs', import.meta.url);writeFileSync(file, "process.on('message',m=>{process.send({id:m.id+1},()=>process.disconnect());});");let c;try {    c = fork(file, [], { env: {}, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] });    const done = once(c, 'close');    const reply = once(c, 'message');    c.send({ id: 3 });    console.log((await reply)[0].id);    await done;}finally {    if (c?.exitCode === null && c.signalCode === null)        c.kill();    unlinkSync(file);}
Node.js Reference