Typorithm · medium

child_process

node-child-processnodejschild-processspawnexec
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn child_process

0%

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.

01

spawn streams output, no shell — for long-running / large output.

02

exec runs a shell and buffers output — mind maxBuffer.

03

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));

Read the implementation

Connect each part of the code to the concept before you type.

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";

2. spawn streams

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 shell

3. Read the stream

Because 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));

4. exec buffers

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, shell

5. fork + IPC

fork 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 + IPC

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

child_process: Lesson & Practice · Typorithm | ApplyDjinn