What is the child_process module in Node.js?
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.
Previous
What is the crypto module in Node.js?
Next
What is the difference between setTimeout and setImmediate in Node.js?