Bun.spawn() and Bun.spawnSync() for spawning child processes. Both APIs use posix_spawn(3) under the hood, making process spawning 60% faster than Node.js’s child_process module.
Bun.spawn()
Pass the command as an array of strings.Bun.spawn() returns a Subprocess object immediately.
Options
Configure the subprocess with a second argument:Stdin
By default,stdin is null (no input). Configure it with the stdin option:
Passing a Response as stdin
Writing incrementally with “pipe”
Passing a ReadableStream as stdin
Stdout and stderr
By default,stdout is "pipe" (a ReadableStream) and stderr is "inherit" (passed through to the parent).
Exit handling
onExit callback
exited promise
proc.exited is a Promise that resolves to the exit code when the process finishes:
Killing a process
Detaching a process
By default, the parentbun process waits for all child processes to exit. Use proc.unref() to detach the child from the parent’s lifetime:
Timeout and AbortSignal
Automatic timeout
SIGTERM. Use killSignal to specify a different signal:
AbortSignal
Resource usage
After a process exits, inspect its resource consumption:Inter-process communication (IPC)
Bun supports a direct IPC channel between twobun processes. Specify an ipc handler when spawning:
parent.ts
process.send() and process.on("message"):
child.ts
Serialization format
Theserialization option controls how messages are encoded between processes:
IPC between Bun and Node.js
Setserialization: "json" when communicating with a Node.js process:
bun-node-ipc.js
Terminal (PTY) support
For interactive terminal applications, spawn a subprocess with a pseudo-terminal attached using theterminal option:
terminal, the subprocess sees process.stdout.isTTY as true, and proc.stdin / proc.stdout / proc.stderr are null — use proc.terminal instead.
PTY support is only available on POSIX systems (Linux and macOS). It is not available on Windows.
Bun.spawnSync()
Bun.spawnSync() is a synchronous, blocking equivalent of Bun.spawn(). It returns a SyncSubprocess with stdout and stderr as Buffers.
maxBuffer
Limit the number of bytes of output before the process is killed:Node.js compatibility
Bun is fully compatible with Node.js’schild_process module:
child_process.fork(), use process.send() and process.on("message") in the child — the same API as Bun’s native IPC.