The Child Process module allows Node.js applications to start and communicate with other processes. It is useful for running system commands, executing separate programs, and handling tasks outside the main JavaScript process.
A child process is a separate operating-system process started
by another process. In Node.js, the built-in
child_process module provides methods for creating
child processes.
Common uses include:
const { exec } = require("child_process");
console.log("Child Process module is ready");
Child Process module is ready
The child_process module is built into Node.js, so no additional package installation is required.
Node.js provides several ways to start child processes. The most common methods are listed below.
| Method | Purpose | Typical Use |
|---|---|---|
exec() |
Runs a command in a shell and collects its output. | Short commands with limited output. |
execFile() |
Runs an executable directly without a shell by default. | Executing a known program. |
spawn() |
Starts a process with streaming input and output. | Commands that produce ongoing output. |
fork() |
Starts another Node.js process with an IPC channel. | Communicating between Node.js processes. |
The exec() method runs a command in a shell and
collects its output. When the command completes, its callback
receives an error, standard output, and standard error.
const { exec } = require("child_process");
exec("node --version", function(error, stdout, stderr) {
if (error) {
console.log("Error:", error.message);
return;
}
if (stderr) {
console.log("Standard error:", stderr);
}
console.log("Node.js version:", stdout.trim());
});
Node.js version: v22.0.0
The displayed version depends on the Node.js version installed on your computer.
The execFile() method executes a program directly
without using a shell by default. Arguments are supplied separately,
which is often safer than building a shell command string.
const { execFile } = require("child_process");
execFile(
process.execPath,
["--version"],
function(error, stdout, stderr) {
if (error) {
console.log("Error:", error.message);
return;
}
console.log("Version:", stdout.trim());
}
);
Version: v22.0.0
process.execPath provides the path to the
currently running Node.js executable. The version shown
depends on your installation.
The spawn() method starts a child process and exposes
its standard output and standard error as streams. This is useful
when a command produces output over time.
const { spawn } = require("child_process");
const child = spawn(
process.execPath,
["-e", "console.log('Hello from child process')"]
);
child.stdout.on("data", function(data) {
console.log("Output:", data.toString().trim());
});
child.stderr.on("data", function(data) {
console.log("Error output:", data.toString());
});
child.on("error", function(error) {
console.log("Failed to start:", error.message);
});
child.on("close", function(code) {
console.log("Child process closed with code:", code);
});
Output: Hello from child process
Child process closed with code: 0
The close event occurs after the process has ended and its standard I/O streams have closed. A code of 0 usually indicates successful completion.
The fork() method is designed for starting another
Node.js process. It creates an inter-process communication (IPC)
channel so the parent and child can send messages to each other.
Create a file named child.js:
process.on("message", function(message) {
console.log("Child received:", message);
if (process.send) {
process.send("Hello from child.js");
}
});
Now create parent.js in the same folder:
const { fork } = require("child_process");
const child = fork("./child.js");
child.on("message", function(message) {
console.log("Parent received:", message);
child.disconnect();
});
child.on("error", function(error) {
console.log("Child error:", error.message);
});
child.send("Hello from parent.js");
Child received: Hello from parent.js
Parent received: Hello from child.js
The parent sends a message to the child, and the child replies using process.send().
A child process can fail to start, exit with a nonzero code, or produce errors. You should handle process events and inspect the exit status when needed.
const { spawn } = require("child_process");
const child = spawn(
process.execPath,
["-e", "process.exit(1)"]
);
child.on("error", function(error) {
console.log("Failed to start:", error.message);
});
child.on("close", function(code, signal) {
if (code !== 0) {
console.log("Child process failed.");
console.log("Exit code:", code);
} else {
console.log("Child process completed successfully.");
}
});
Child process failed.
Exit code: 1
The child deliberately exits with code 1 in this example. A nonzero exit code usually indicates that the command did not complete successfully.
| Feature | exec() | spawn() | fork() |
|---|---|---|---|
| Shell by default | Yes | No | No shell command |
| Output handling | Collected by callback | Streams | IPC and standard streams |
| Designed for | Shell commands | Programs and commands | Node.js scripts |
| IPC messaging | Not by default | Not by default | Yes |
node --version using exec().Select the correct answer for each question.