Skip to main content
Bun provides Bun.spawn() for launching child processes with full control over stdio, environment variables, and process lifecycle.

Basic Usage

Synchronous Spawning

Use Bun.spawnSync() for blocking process execution:

Options

Command and Arguments

Working Directory

Environment Variables

Standard I/O

Control stdin, stdout, and stderr:
Options:
  • "pipe" - Capture the stream (default)
  • "inherit" - Pass through from parent process
  • "ignore" - Discard the stream
  • Bun.file(path) - Read from or write to a file
  • TypedArray | ArrayBuffer - Use a buffer
  • ReadableStream - Pipe from a stream (stdin only)
  • number - Use a file descriptor

Piping to stdin

IPC (Inter-Process Communication)

Enable message passing between parent and child:
In the child process:

Subprocess Object

The returned Subprocess object has these properties:

Process Information

Streams

Access stdio streams:

Process Control

Resource Usage

Get CPU and memory statistics:

Timeouts and Abortion

Using AbortSignal

Built-in Timeout

Error Handling

Differences from Node.js

Simpler API

Bun’s spawn is more streamlined than Node’s child_process:

Web Streams

Bun uses Web Standard ReadableStream and WritableStream instead of Node.js streams.

Automatic Buffer Management

Bun efficiently handles large outputs without manual buffer management.

Performance Tips

  1. Use spawnSync for quick commands - Avoid async overhead for fast operations
  2. Pipe directly to files - Use Bun.file() for stdin/stdout instead of buffering
  3. Stream large outputs - Don’t wait for exited if you only need stdout
  4. Reuse environment - Pass env: process.env to avoid copying

Platform Support

Bun.spawn() works on:
  • macOS (x64, ARM64)
  • Linux (x64, ARM64)
  • Windows (x64, ARM64)
On Windows, Bun automatically handles path resolution and uses cmd.exe for shell scripts.