typescriptintermediate
Child Process: exec and spawn
Execute shell commands and spawn processes with exec, execFile, spawn, and fork in Node.js.
typescriptPress ⌘/Ctrl + Shift + C to copy
import { exec, execFile, spawn, fork } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);
// exec: run shell command, buffer output
async function runCommand(cmd: string): Promise<string> {
const { stdout, stderr } = await execAsync(cmd);
if (stderr) console.error('stderr:', stderr);
return stdout.trim();
}
// execFile: safer, no shell interpolation
function listFiles(dir: string): Promise<string> {
return new Promise((resolve, reject) => {
execFile('ls', ['-la', dir], (error, stdout) => {
if (error) reject(error);
else resolve(stdout);
});
});
}
// spawn: streaming output (for long-running processes)
function streamProcess(cmd: string, args: string[]): Promise<number> {
return new Promise((resolve, reject) => {
const child = spawn(cmd, args, { stdio: 'pipe' });
child.stdout.on('data', (data: Buffer) => {
process.stdout.write(`[stdout] ${data}`);
});
child.stderr.on('data', (data: Buffer) => {
process.stderr.write(`[stderr] ${data}`);
});
child.on('close', (code) => {
resolve(code ?? 1);
});
child.on('error', reject);
});
}
// spawn with inherit (pass through stdio)
function runInteractive(cmd: string, args: string[]): Promise<number> {
return new Promise((resolve, reject) => {
const child = spawn(cmd, args, {
stdio: 'inherit', // inherit parent's stdio
shell: true,
});
child.on('close', (code) => resolve(code ?? 1));
child.on('error', reject);
});
}
// Usage
const nodeVersion = await runCommand('node --version');
console.log('Node:', nodeVersion);
const gitBranch = await runCommand('git branch --show-current');
console.log('Branch:', gitBranch);
await streamProcess('echo', ['Hello from spawn']);
// Run npm install with inherited output
// await runInteractive('npm', ['install']);Use Cases
- Running shell commands from Node.js
- Build tool orchestration
- Process management and scripting
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptadvanced
Node.js Worker Threads for Parallel Processing
Use Worker Threads to run CPU-intensive tasks in parallel without blocking the event loop.
Best for: CPU-intensive data processing without blocking
#nodejs#worker-threads
typescriptintermediate
Node.js Stream Pipeline with Transform
Build efficient data processing pipelines using Node.js streams for large file handling.
Best for: Processing large CSV files without loading into memory
#nodejs#streams
typescriptintermediate
TypeScript Typed Event Emitter
Create type-safe event emitters in Node.js with full TypeScript support and autocomplete.
Best for: Type-safe pub/sub communication between modules
#nodejs#events
typescriptintermediate
Node.js Graceful Shutdown Handler
Implement graceful shutdown to properly close connections and finish requests before exit.
Best for: Production Node.js server reliability
#nodejs#shutdown