child_process
JavaScript · Reference cheat sheet
child_process
JavaScript · Reference cheat sheet
📋 Overview
Node.js node:child_process runs external programs. Prefer spawn / execFile with argument arrays over shell exec when you control the command. Shell mode invites injection if user input is interpolated.
import { spawn, execFile, exec } from "node:child_process";🔧 Core concepts
spawn(command, args[], options?): streams stdio; best for long-running or large output.execFile(file, args[], options?, cb): runs executable directly (no shell); buffered stdout/stderr.exec(command, options?, cb): runs via shell — convenient, riskier.- Promises:
import \{ spawn \} from "node:child_process"+ manual wrappers, orpromisify(execFile). - Options:
cwd,env,timeout,maxBuffer,stdio('pipe' | 'inherit' | 'ignore'). - Exit: listen for
close/exit; checkcodeandsignal.
💡 Examples
import { spawn, execFile } from "node:child_process";
import { promisify } from "node:util";
const execFileAsync = promisify(execFile);
// Safe: args array, no shell
const { stdout } = await execFileAsync("node", ["-v"]);
console.log(stdout.trim());
const child = spawn("node", ["script.js", "--port", "9000"], {
stdio: "inherit",
cwd: process.cwd(),
env: { ...process.env, NODE_ENV: "development" },
});
child.on("close", (code) => {
console.log("exit", code);
});
// Capture streamed output
const p = spawn("git", ["status", "--porcelain"]);
let out = "";
p.stdout.setEncoding("utf8");
p.stdout.on("data", (chunk) => {
out += chunk;
});
await new Promise((resolve, reject) => {
p.on("error", reject);
p.on("close", (code) => (code === 0 ? resolve() : reject(new Error(`code ${code}`))));
});// Avoid: exec with untrusted input
// exec(`ls ${userPath}`); // injection risk⚠️ Pitfalls
exec/execSyncshell injection: never concatenate untrusted strings into the command line.maxBuffer:exec/execFilethrow if output exceeds limit (default ~1MB) — usespawnfor big output.- Windows:
.cmd/.batoften needshell: trueorexec; prefer explicit.exewhen possible. - Forgotten listeners / open stdio can keep the parent process alive.
spawndoes not reject on non-zero exit by default — checkcodeyourself.