1. The four ways
child_process offers spawn, exec, execFile, and fork — four ways to launch a separate process, each with different tradeoffs.
import { spawn, exec, execFile, fork } from "node:child_process";Typorithm · medium
Step 1 of 8 · intro card
Concurrency
child_process launches external processes four ways: spawn streams output and runs without a shell (best for long-running commands and large output); exec runs a shell and buffers all output (watch the default maxBuffer); execFile runs a binary directly without a shell; and fork spawns a Node child with an IPC message channel.
spawn launches a command directly (no shell) and exposes stdout/stderr as streams, so it handles long-running processes and large or continuous output without buffering it all in memory. execFile is similar but tuned for running a single binary with arguments and no shell.
exec runs your command string through a shell, so pipes, globs, and redirection work, but it buffers the entire output and hands it to a callback when the process exits. That buffering is the catch: output beyond the default maxBuffer (about 1 MB) makes exec error out, so it suits short commands with small output.
fork is a specialization for spawning another Node.js script: it sets up an IPC channel automatically, letting parent and child pass messages with process.send and the "message" event — useful for offloading work to a separate Node process.
spawn streams output, no shell — for long-running / large output.
exec runs a shell and buffers output — mind maxBuffer.
fork spawns a Node child with an IPC message channel.
import { spawn, exec, execFile, fork } from "node:child_process";
const ls = spawn("ls", ["-la"]); // streams, no shell
ls.stdout.on("data", (chunk) => process.stdout.write(chunk));
exec("ls -la | grep .ts", (err, stdout) => console.log(stdout)); // buffers, shell
execFile("node", ["--version"], (err, out) => console.log(out));
const child = fork("./worker.js"); // Node child + IPC
child.on("message", (msg) => console.log("from child", msg));Connect each part of the code to the concept before you type.
child_process offers spawn, exec, execFile, and fork — four ways to launch a separate process, each with different tradeoffs.
import { spawn, exec, execFile, fork } from "node:child_process";spawn streams stdout/stderr and runs the command directly without a shell, ideal for long-running or large output.
const ls = spawn("ls", ["-la"]); // streams, no shellBecause spawn streams, you handle output chunk by chunk as it arrives rather than waiting for the process to finish.
ls.stdout.on("data", (chunk) => process.stdout.write(chunk));exec runs the command in a shell (so pipes and globs work) and buffers all output into a callback once the process exits.
exec("ls -la | grep .ts", (err, stdout) => console.log(stdout)); // buffers, shellfork spawns a new Node process running a JS file with a built-in IPC channel, so parent and child exchange messages via process.send / on("message").
const child = fork("./worker.js"); // Node child + IPCRead the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.