Asynchronous command execution
Run an executable while streaming its output and keeping the event loop responsive.
Node.js · Module
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.
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.
import { spawn, spawnSync, exec, execFile, fork, execSync, execFileSync } from 'node:child_process';| Name | Type | Description |
|---|---|---|
method arguments | varies by method | The command or executable to run, plus method-specific arguments and options; exact parameters depend on the selected API. |
Asynchronous creation methods return a ChildProcess instance; synchronous methods return method-specific completion results.
node:child_process to create and manage subprocesses.spawn() for asynchronous direct process creation when the parent must remain responsive.ChildProcess object for lifecycle handling and stream access.execFile() for direct command execution without shell startup.fork() to launch a Node.js module with an interprocess communication channel.spawnSync() waits until the child exits or is terminated.exec() as shell execution: interpolated or untrusted command text can create command-injection risk, so prefer direct process creation when shell syntax is unnecessary.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.
Run an executable while streaming its output and keeping the event loop responsive.
Execute a shell command and process its output after completion.
Start a separate Node.js module that communicates with its parent.
Perform a short, controlled operation where blocking is acceptable.
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.
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.
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.
ChildProcessspawn()spawnSync()exec()execFile()fork()execSync()execFileSync()