A lightweight, fluent API for spawning child processes in Node.js. Enhanced wrapper around child_process.spawn with better defaults and convenience methods.
- π οΈ Fluent API - Chainable command building with
SpawnBuilder - π§ Multiple Modes -
spawn,spawnSilent,spawnStreamfor different use cases - π‘ Enhanced Options - Extended spawn options with encoding support
- π Type Safe - Full TypeScript support with strict typing
- π Lightweight - Minimal wrapper around native child_process
- π» Cross-Platform - Consistent behavior across Windows, macOS, Linux
npm install @dephub/spawn
# or
pnpm add @dephub/spawn
# or
yarn add @dephub/spawnimport { spawn, spawnStream, spawnSilent, SpawnBuilder } from '@dephub/spawn';
// Basic spawn with output capture
const result = await spawn('ls', ['-la']);
console.log(result.stdout);
// Stream output to parent process
await spawnStream('npm', ['install']);
// Silent execution with captured output
const silentResult = await spawnSilent('node', ['script.js']);
// Fluent builder API
const builderResult = await new SpawnBuilder()
.command('ls')
.arg('-la')
.setOption('cwd', './src')
.run();import { spawn } from '@dephub/spawn';
// Run simple command
const { code, stdout, stderr } = await spawn('echo', ['hello world']);
// Check exit code
if (code === 0) {
console.log('Success:', stdout);
} else {
console.error('Error:', stderr);
}import { spawn } from '@dephub/spawn';
// Execute in specific directory
const result = await spawn('ls', ['-la'], {
cwd: './src',
env: { ...process.env, NODE_ENV: 'production' },
});import { spawnStream } from '@dephub/spawn';
// Stream npm install output to console
await spawnStream('npm', ['install'], {
cwd: './project',
});
// Note: spawnStream returns only exit code, not stdout/stderrimport { spawnSilent } from '@dephub/spawn';
// Capture output without inheriting stdio
const { stdout } = await spawnSilent('git', ['status']);
console.log('Git status:', stdout);import { SpawnBuilder } from '@dephub/spawn';
// Complex command with fluent API
const result = await new SpawnBuilder()
.command('ffmpeg')
.arg('-i')
.arg('input.mp4')
.arg('-c:v')
.arg('libx264')
.arg('output.mp4')
.setOption('stdio', 'pipe')
.setOption('encoding', 'utf8')
.run();import { spawner } from '@dephub/spawn';
// Reusable spawner instance
const result = await spawner
.command('docker')
.arg('build')
.arg('-t')
.arg('my-app')
.arg('.')
.run();Execute command with output capture (stdio: 'pipe' by default).
Execute with piped stdio and captured output.
Execute with inherited stdio (output streamed to parent).
Fluent API for building commands:
.command(cmd)- Set command to execute.arg(arg)- Add command argument.setOption(key, value)- Set spawn option.run()- Execute and return result
Extends Node.js SpawnOptions with:
encoding- Character encoding for stdout/stderr
code- Exit code (0 for success)stdout- Captured stdout outputstderr- Captured stderr output
import { spawn } from '@dephub/spawn';
try {
const result = await spawn('unknown-command');
} catch (error) {
console.error('Process failed:', error.message);
}import { spawn } from '@dephub/spawn';
spawn('ls', ['-la'])
.then((result) => console.log(result.stdout))
.catch((error) => console.error('Error:', error));import { spawn } from '@dephub/spawn';
const commands = [
['ls', ['-la']],
['pwd', []],
['whoami', []],
];
const results = await Promise.all(
commands.map(([cmd, args]) => spawn(cmd, args)),
);import { spawnStream } from '@dephub/spawn';
async function buildProject() {
await spawnStream('npm', ['run', 'build']);
await spawnStream('npm', ['test']);
await spawnStream('npm', ['run', 'lint']);
}import { spawn } from '@dephub/spawn';
import { readFile } from '@dephub/read';
const { stdout } = await spawn('git', ['log', '--oneline']);
const commits = stdout.split('\n').filter(Boolean);#!/usr/bin/env node
import { spawnStream } from '@dephub/spawn';
await spawnStream('docker', ['build', '-t', 'my-app', '.']);
await spawnStream('docker', ['push', 'my-app']);import { spawn } from 'child_process';
const child = spawn('ls', ['-la'], { stdio: 'pipe' });
let stdout = '';
child.stdout.on('data', (data) => {
stdout += data;
});
await new Promise((resolve) => {
child.on('close', resolve);
});import { spawn } from '@dephub/spawn';
const { stdout } = await spawn('ls', ['-la']);MIT License β see LICENSE for details.
Author: Estarlin R (estarlincito.com)