Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Function

fork

child_process.fork() starts a new Node.js process to run a specified module and provides a built-in IPC channel for parent-child communication. It returns a ChildProcess instance.

How it works

fork() starts a new Node.js process to execute a specified module. It returns a ChildProcess with an IPC channel, allowing the parent and child to exchange messages; unlike the POSIX fork(2) call, it does not clone the current process.

Syntax

child_process.fork(modulePath[, args][, options])

Parameters

NameTypeDescription
modulePathstring | URLThe module to run in the child.
argsstring[]List of string arguments.
optionsObjectOptions controlling the child process, including its working directory.

Returns

A ChildProcess instance with an additional built-in IPC communication channel.

Key points

  • It starts a separate Node.js process, runs the requested module, and establishes IPC between the parent and child.
  • The module path can be supplied as a string or URL and determines the child process entry point.
  • Optional arguments are provided as an array of strings and become command-line arguments for the child.
  • The child’s working directory can be configured with a string or URL through the options object.
  • The call returns a ChildProcess, which can be monitored and controlled using the child-process API.
  • The built-in IPC channel supports two-way message exchange between the parent and the forked child.

Common mistakes

  • Do not treat this as a memory-cloning primitive: it launches another Node.js process rather than duplicating the current process state.

Example: Fork a Node.js worker with IPC

// parent.mjs
import { fork } from 'node:child_process';

const child = fork(new URL('./child.mjs', import.meta.url));
child.on('message', (message) => {
  console.log(message);
  child.disconnect();
});

// child.mjs
process.on('message', (message) => {
  process.send?.({ result: message.value * 2 });
});

process.send?.({ result: 42 });

The parent starts a separate Node.js process for the child module and receives data through the automatically established IPC channel.

When to use it

Process isolation

Split CPU-heavy or isolated application work into a separate Node.js process while retaining structured parent-child messaging.

Message-based workers

Create a dedicated child entry point that receives commands from a parent and returns results through IPC.

Configured child execution

Run a Node.js module with a controlled child working directory and command-line arguments.

Interview questions

QWhat does child_process.fork() do?

It launches a new Node.js process for the specified module and returns a ChildProcess. The returned process includes an IPC channel for parent-child messaging.

Follow-up: How would you exchange data? Listen for message events in the parent and use the child process messaging APIs from the child to send values back.

QHow does fork() differ from spawn()?

fork() is a specialized form of spawn() for starting Node.js processes. Its distinguishing feature is the built-in IPC communication channel.

Follow-up: When would spawn() be preferable? Use it when launching a general executable or when the specialized Node.js-module behavior and built-in IPC are not needed.

QWhat are the main inputs to fork()?

The first argument identifies the module that the child executes. Optional arguments are passed as strings, while options configure child-process behavior such as its working directory.

Follow-up: What should be checked when resolving the module? Ensure the module path or URL identifies the intended child entry point and that its runtime assumptions match the child process.

  • child_process.spawn
  • ChildProcess
  • child_process.exec
  • child_process.execFile
  • child_process.execSync

Source: Child Process. Checked 2026-10-15.