Node.js Child Processes: Escaping the Main Thread
A child process lets your Node.js app run external commands without blocking the event loop. Use it for CPU-intensive tasks like image processing or running system utilities. The footgun: using synchronous versions (`execSync`) will block your entire server.
WHY IT EXISTS Node.js runs on a single-threaded event loop. This is highly efficient for I/O-bound tasks but means that any CPU-intensive operation will block the entire application, preventing it from handling other requests. Child processes solve this by offloading heavy work to a separate OS process, keeping the main thread free.
THE MENTAL MODEL Think of your main Node.js application as a manager. When a task arrives that's too slow or complex to handle directly (like resizing a thousand images), the manager delegates it to a specialized worker in a separate office—a child process. The manager can continue their own work and get notified when the worker is done, or receive updates via a communication channel. This prevents one heavy task from bringing the entire operation to a standstill.
HOW IT WORKS The child_process module provides several functions to create subprocesses. The two main asynchronous ones are spawn and exec. spawn is ideal for long-running processes with large outputs, as it streams stdout and stderr. exec is a convenience function that buffers the entire output before returning it in a callback, making it simpler for commands with smaller, finite outputs. There's also fork, a special version of spawn for running other Node.js scripts, which includes a built-in communication channel for passing messages.
WHEN TO USE IT Use child processes for tasks that would otherwise block the event loop. Three key scenarios are: first, running external system commands (e.g., git pull, ffmpeg -i ...); second, performing heavy computations like video encoding or large file transformations; and third, parallelizing work across multiple CPU cores by forking your Node script.
WHEN NOT TO USE IT For I/O-bound concurrency or CPU-bound tasks that can benefit from shared memory, worker_threads are often a better choice as they have lower overhead. Critically, avoid using synchronous child process methods (execSync, spawnSync) inside a web server or any long-running application, as they will block the entire process and kill performance.
ONE CANONICAL EXAMPLE To list files in a directory without blocking, you can use spawn to run the ls -lh command. The parent process listens for 'data' events on the child's stdout stream to process the output as it arrives, piece by piece. const { spawn } = require('node:child_process'); const ls = spawn('ls', ['-lh', '/usr']); ls.stdout.on('data', (data) => { console.log(Output chunk: ${data}); }); ls.on('close', (code) => { console.log(Process exited with code ${code}); });
Read the original → nodejs.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.