What are Child Processes in Node.js?
Learn what child processes are in Node.js, when to use spawn, exec, execFile and fork, how IPC works, and common interview questions with clear examples.
Expected Interview Answer
Child processes let a Node.js program spawn separate operating-system processes to run other programs or additional Node scripts, so CPU-heavy or blocking work happens outside the single main event loop.
The built-in child_process module offers four ways to create them: spawn (streams output, good for large or long-running commands), exec (buffers output, convenient for short shell commands), execFile (runs a binary directly without a shell), and fork (a specialized spawn for new Node.js processes with a built-in IPC channel). Because each child has its own memory and V8 instance, they run truly in parallel across CPU cores and communicate with the parent through streams or message passing rather than shared memory.
- Runs CPU-bound work without blocking the main event loop
- Uses multiple CPU cores for real parallelism
- Isolates crashes so one failing task does not kill the parent
- Lets Node invoke shell commands and external programs
- fork enables message-passing between Node processes via IPC
AI Mentor Explanation
A team captain cannot personally bat, bowl, and field at once, so he delegates: a specialist bowler runs in while the captain keeps directing play. Each teammate works in parallel on their own task and reports the result back. Child processes are those specialists — the main Node process hands heavy work to separate players and stays free to coordinate the match.
Step-by-Step Explanation
Step 1
Import the module
Require child_process and pick spawn, exec, execFile, or fork based on the task.
Step 2
Choose spawn vs exec
Use spawn for streamed, long-running or large output; use exec for short commands whose buffered output fits in memory.
Step 3
Launch the child
Call the method with the command and an args array, e.g. spawn('node', ['worker.js']).
Step 4
Wire up communication
Read child.stdout / child.stderr streams, or with fork use child.send() and child.on('message') for structured IPC.
Step 5
Handle lifecycle events
Listen for 'error', 'close', and 'exit' to detect failures and read the exit code.
What Interviewer Expects
- Knows the four creation methods and when each fits
- Explains that each child is a separate OS process with its own V8 and memory
- Understands spawn streams vs exec buffering and the maxBuffer risk
- Describes IPC via fork's send/message channel
- Connects child processes to CPU-bound work and multi-core usage
Common Mistakes
- Confusing child processes with worker threads (threads share memory, processes do not)
- Using exec for huge output and hitting the maxBuffer limit
- Assuming spawn runs commands through a shell by default
- Forgetting to handle 'error' and 'exit' events
- Thinking fork can run any binary rather than only Node.js scripts
Best Answer (HR Friendly)
“A child process is a way for a Node.js app to start a separate program to do heavy work, so the main app stays responsive. It is like a manager handing a big task to an assistant who works on it separately and reports back when done.”
Code Example
const { spawn, fork } = require('child_process');
// spawn: run an external command and stream its output
const ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', (data) => console.log(`output: ${data}`));
ls.stderr.on('data', (data) => console.error(`error: ${data}`));
ls.on('close', (code) => console.log(`exited with code ${code}`));
// fork: run another Node script with an IPC channel
const child = fork('./worker.js');
child.send({ start: true, jobId: 42 });
child.on('message', (msg) => console.log('from child:', msg));Follow-up Questions
- What is the difference between spawn and exec?
- How do child processes differ from worker threads?
- How does fork enable communication between Node processes?
- What is the maxBuffer limit in exec and why does it matter?
- How does the cluster module build on child_process?
MCQ Practice
1. Which child_process method streams output and suits long-running commands?
spawn returns streams for stdout/stderr, making it ideal for large or long-running output instead of buffering it all in memory.
2. Which method creates a new Node.js process with a built-in IPC channel?
fork is a special case of spawn dedicated to Node scripts and automatically sets up a message-passing channel between parent and child.
3. Why can exec fail on commands with very large output?
exec buffers the entire output; exceeding the maxBuffer limit throws an error, which is why spawn is preferred for large output.
Flash Cards
Which four methods create child processes? — spawn, exec, execFile, and fork from the child_process module.
spawn vs exec? — spawn streams output for long/large tasks; exec buffers output for short commands.
What makes fork special? — It launches a new Node.js process and sets up an IPC channel for send()/message.
Do child processes share memory? — No — each has its own memory and V8 instance; they communicate via streams or IPC.