child_process Module (Node.js)
child_process is Node.js's built-in module for spawning external programs and shell commands from your app. spawn, exec, execFile, and fork let you run other processes and communicate with them.
Node.js
Sometimes you need to run something outside Node — a shell command, a Python script, an image tool like ffmpeg. The child_process module launches these as separate OS processes and lets your Node app feed them input and read their output.
It offers four main functions. spawn streams a long-running process's output (best for large or continuous output, since it does not buffer everything). exec runs a command in a shell and buffers the result (convenient for short commands, but risky with untrusted input due to shell injection). execFile runs a binary directly without a shell (safer). fork spawns another Node script with a built-in messaging channel.
Prefer spawn or execFile with an argument array over exec when any input is user-supplied, to avoid shell-injection vulnerabilities. For running more JavaScript in parallel you would use fork or worker threads rather than launching a whole new node via exec.
Syntax
const { spawn } = require("child_process");Examples
JavaScript
const { spawn } = require("child_process");
const ls = spawn("ls", ["-la"]);
ls.stdout.on("data", d => process.stdout.write(d));
ls.on("close", code => console.log("exited with", code));