Bun.spawn() for launching child processes with full control over stdio, environment variables, and process lifecycle.
Basic Usage
Synchronous Spawning
UseBun.spawnSync() for blocking process execution:
Options
Command and Arguments
Working Directory
Environment Variables
Standard I/O
Control stdin, stdout, and stderr:"pipe"- Capture the stream (default)"inherit"- Pass through from parent process"ignore"- Discard the streamBun.file(path)- Read from or write to a fileTypedArray | ArrayBuffer- Use a bufferReadableStream- 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:Subprocess Object
The returnedSubprocess 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’schild_process:
Web Streams
Bun uses Web StandardReadableStream and WritableStream instead of Node.js streams.
Automatic Buffer Management
Bun efficiently handles large outputs without manual buffer management.Performance Tips
- Use
spawnSyncfor quick commands - Avoid async overhead for fast operations - Pipe directly to files - Use
Bun.file()for stdin/stdout instead of buffering - Stream large outputs - Don’t wait for
exitedif you only need stdout - Reuse environment - Pass
env: process.envto avoid copying
Platform Support
Bun.spawn() works on:
- macOS (x64, ARM64)
- Linux (x64, ARM64)
- Windows (x64, ARM64)
cmd.exe for shell scripts.