What is the child_process module in Node.js?

Why Interviewers Ask This

This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Node.js topics. It also reveals how well you can explain technical ideas to non-experts.

Answer

The child_process module enables spawning child processes to run external commands, scripts, or programs from within Node.js. Key methods: exec(command, callback) — runs a shell command, buffers output, invokes callback with stdout/stderr strings (limited to 200KB by default); execFile(file, args, callback) — like exec but directly executes a file without a shell (more secure); spawn(command, args) — spawns a process and streams stdin/stdout/stderr in real-time (no buffer limit, suitable for long-running processes or large output); fork(modulePath) — special case of spawn for Node.js scripts — creates a new Node.js process with an IPC (inter-process communication) channel for sending messages between parent and child. Use child_process for: running shell commands, executing Python/Ruby scripts, CPU-intensive tasks in a separate process (though Worker Threads are better for that), and running system utilities.

Common Mistake

Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex Node.js answers easy to follow.