npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@dephub/spawn

v1.0.1

Published

A lightweight, fluent API for spawning child processes in Node.js with enhanced options and convenience methods

Readme

@dephub/spawn 🚀

A lightweight, fluent API for spawning child processes in Node.js. Enhanced wrapper around child_process.spawn with better defaults and convenience methods.

NPM version ESM-only


Features ✨

  • 🛠️ Fluent API - Chainable command building with SpawnBuilder
  • 🔧 Multiple Modes - spawn, spawnSilent, spawnStream for 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

Installation 📦

npm install @dephub/spawn
# or
pnpm add @dephub/spawn
# or
yarn add @dephub/spawn

Quick Start 🚀

import { 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();

Usage Examples 🎯

Basic Command Execution

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);
}

Working Directory and Environment

import { spawn } from '@dephub/spawn';

// Execute in specific directory
const result = await spawn('ls', ['-la'], {
  cwd: './src',
  env: { ...process.env, NODE_ENV: 'production' },
});

Streaming Output

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/stderr

Silent Execution

import { spawnSilent } from '@dephub/spawn';

// Capture output without inheriting stdio
const { stdout } = await spawnSilent('git', ['status']);
console.log('Git status:', stdout);

Fluent Builder API

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();

Pre-configured Spawner

import { spawner } from '@dephub/spawn';

// Reusable spawner instance
const result = await spawner
  .command('docker')
  .arg('build')
  .arg('-t')
  .arg('my-app')
  .arg('.')
  .run();

API Reference 📚

Core Functions

spawn(command, args?, options?)

Execute command with output capture (stdio: 'pipe' by default).

spawnSilent(command, args?, options?)

Execute with piped stdio and captured output.

spawnStream(command, args?, options?)

Execute with inherited stdio (output streamed to parent).

SpawnBuilder Class

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

Types

SpawnOptions

Extends Node.js SpawnOptions with:

  • encoding - Character encoding for stdout/stderr

SpawnResult

  • code - Exit code (0 for success)
  • stdout - Captured stdout output
  • stderr - Captured stderr output

Common Patterns 🎨

Error Handling

import { spawn } from '@dephub/spawn';

try {
  const result = await spawn('unknown-command');
} catch (error) {
  console.error('Process failed:', error.message);
}

Working with Promises

import { spawn } from '@dephub/spawn';

spawn('ls', ['-la'])
  .then((result) => console.log(result.stdout))
  .catch((error) => console.error('Error:', error));

Batch Operations

import { spawn } from '@dephub/spawn';

const commands = [
  ['ls', ['-la']],
  ['pwd', []],
  ['whoami', []],
];

const results = await Promise.all(
  commands.map(([cmd, args]) => spawn(cmd, args)),
);

Integration Examples 🔗

With Build Tools

import { spawnStream } from '@dephub/spawn';

async function buildProject() {
  await spawnStream('npm', ['run', 'build']);
  await spawnStream('npm', ['test']);
  await spawnStream('npm', ['run', 'lint']);
}

With File System Operations

import { spawn } from '@dephub/spawn';
import { readFile } from '@dephub/read';

const { stdout } = await spawn('git', ['log', '--oneline']);
const commits = stdout.split('\n').filter(Boolean);

CLI Tools Integration

#!/usr/bin/env node
import { spawnStream } from '@dephub/spawn';

await spawnStream('docker', ['build', '-t', 'my-app', '.']);
await spawnStream('docker', ['push', 'my-app']);

Migration from Child Process

Before (Node.js native)

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);
});

After (@dephub/spawn)

import { spawn } from '@dephub/spawn';

const { stdout } = await spawn('ls', ['-la']);

License 📄

MIT License – see LICENSE for details.

Author: Estarlin R (estarlincito.com)