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

Node.js · Module

child_process

The node:child_process module lets Node.js start subprocesses asynchronously or synchronously. Its APIs cover direct executable launching, shell commands, Node.js worker processes, and parent-child IPC.

How it works

The node:child_process module starts external programs or additional Node.js processes. Its asynchronous creation methods return ChildProcess objects, whose event and stream interfaces let the parent observe output and lifecycle events without blocking the event loop; synchronous variants wait for completion.

Syntax

import { spawn, spawnSync, exec, execFile, fork, execSync, execFileSync } from 'node:child_process';

Parameters

NameTypeDescription
method argumentsvaries by methodThe command or executable to run, plus method-specific arguments and options; exact parameters depend on the selected API.

Returns

Asynchronous creation methods return a ChildProcess instance; synchronous methods return method-specific completion results.

Key points

  • Import node:child_process to create and manage subprocesses.
  • Use spawn() for asynchronous direct process creation when the parent must remain responsive.
  • Process-creation methods return a ChildProcess object for lifecycle handling and stream access.
  • Attach listeners to a child process to react to events during its lifetime.
  • Choose execFile() for direct command execution without shell startup.
  • Use fork() to launch a Node.js module with an interprocess communication channel.

Common mistakes

  • Use synchronous creation only when deliberately accepting event-loop blocking; spawnSync() waits until the child exits or is terminated.
  • Treat exec() as shell execution: interpolated or untrusted command text can create command-injection risk, so prefer direct process creation when shell syntax is unnecessary.

Example: Run a subprocess and read its output

import { spawn } from 'node:child_process';

const child = spawn(process.execPath, ['-e', 'console.log("hello")']);

child.stdout.on('data', (chunk) => process.stdout.write(chunk));
child.on('close', (code) => console.log(`exit code: ${code}`));

This starts a Node.js subprocess asynchronously, forwards its standard output, and reports the termination code.

When to use it

Asynchronous command execution

Run an executable while streaming its output and keeping the event loop responsive.

Shell-based automation

Execute a shell command and process its output after completion.

Node.js process workers

Start a separate Node.js module that communicates with its parent.

Synchronous subprocess tasks

Perform a short, controlled operation where blocking is acceptable.

Interview questions

QHow does spawn() work, and what does it return?

spawn() starts a subprocess asynchronously and does not block Node.js's event loop. It returns a ChildProcess, which exposes lifecycle events and standard streams for interaction.

Follow-up: When is execFile() preferable? Use it when a command should be launched directly without first creating a shell.

QHow does exec() differ from spawn()?

exec() creates a shell, runs the supplied command through it, and provides collected standard output and standard error to a completion callback. This makes it convenient for shell command strings but different from direct execution.

Follow-up: What is the synchronous counterpart? execSync() performs the analogous shell-based operation while blocking the event loop.

QWhat is special about fork()?

fork() starts another Node.js process for a specified module and establishes an IPC channel between the parent and child. It is intended for Node-to-Node process communication rather than arbitrary executable invocation.

Follow-up: How can the parent observe the child lifecycle? Register listeners on the returned ChildProcess, which follows the EventEmitter API.

  • ChildProcess
  • spawn()
  • spawnSync()
  • exec()
  • execFile()
  • fork()
  • execSync()
  • execFileSync()

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