Child processes in nodejs
medium · Node.js — Server-side JavaScript
In Node.js, both spawn and exec are part of the child_process module and are used to execute system commands or external files. However, they handle data and process lifecycles in fundamentally different ways. 1. child_process.spawn() spawn launches a new process in a separate shell-less environment. It returns a Stream (stdout and stderr), meaning it processes data in chunks. Behavior: It is asynchronous and non-blocking. Data Handling: Best for large amounts of data or long-running processes (like video processing or a continuous server log). Performance: Very memory efficient because it doesn't buffer the output. const { spawn } = require('child_process'); const ls = spawn('ls', ['-lh', '/usr']); ls.stdout.on('data', (data) => { console.log(`stdout: ${data}`); }); ls.on('close', (code) => { console.log(`child process exited with code ${code}`); }); 2. child_process.exec() exec creates a new shell and runs the command within it. It buffers the entire output and passes it to a callback function once the process finishes. Behavior: It is asynchronous but has a buffer limit (default is 200KB). Data Handling: Best for small, quick commands that return a simple string or result. Simplicity: Easier to write because you can use full shell syntax (like pipes and redirects) directly in the command string. const { exec } = require('child_process'); exec('ls -lh /usr', (error, stdout, stderr) => { if (error) { console.error(`exec error: ${error}`); return; } console.log(`stdout: ${stdout}`); }); 3. Key Differences at a Glance Feature spawn exec Output Type Streams (Standard I/O) Buffer (Callback) Data Size Handles massive amounts of data Limited by buffer size (200KB) Process Speed Starts processing immediately Waits for process to finish Shell No shell by default (faster) Creates a shell (allows ` Usage Case Long-running tasks, binary data Short-lived scripts, shell commands In Node.js, both spawn and exec are part of the child_process module and are used to execute system commands or external files. However, they handle data and process lifecycles in fundamentally different ways. 4. When to Use Which? Use spawn when: You are dealing with huge files or streams (like your study material uploads for Careeroza). The output is continuous (like ping or tail -f ). You want the highest possible performance and memory efficiency. Use exec when: You are running a simple command that returns a small amount of text. You need to use shell-specific features like grep | awk or environment variable expansion directly in the string. You want a quick callback and don't care about memory overhead for that specific task 5. Safety Warning: exec and Injection Because exec runs inside a shell, it is vulnerable to Command Injection if you pass user-provided input directly into it. Unsafe: exec('rm -rf ' + userInput) Safe: Use spawn , which passes arguments as an array, making it impossible for a user to "escape" the command and run their own code.