Lesson 24 / 28

Spawning Child Processes

Run other programs from inside Node.

spawn a command

spawn runs an external command and streams its output — handy for wrapping CLI tools like git or ffmpeg.

import { spawn } from "node:child_process";

const ls = spawn("ls", ["-la"]);

ls.stdout.on("data", (chunk) => console.log(chunk.toString()));
ls.on("close", (code) => console.log(`child exited with ${code}`));

spawn vs exec

spawn streams output chunk by chunk (good for large output); exec buffers it all and gives you one callback (simpler for short commands).