Skip to main content
Bun provides 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).
Configure output streams with the following values:

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 parent bun 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

By default, timed-out processes are killed with 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 two bun processes. Specify an ipc handler when spawning:
parent.ts
In the child process, use process.send() and process.on("message"):
child.ts

Serialization format

The serialization option controls how messages are encoded between processes:

IPC between Bun and Node.js

Set serialization: "json" when communicating with a Node.js process:
bun-node-ipc.js
To disconnect the IPC channel from the parent:

Terminal (PTY) support

For interactive terminal applications, spawn a subprocess with a pseudo-terminal attached using the terminal option:
When using 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.
Use Bun.spawn() for HTTP servers and long-running applications. Use Bun.spawnSync() for CLI tools and scripts where blocking is acceptable.

maxBuffer

Limit the number of bytes of output before the process is killed:

Node.js compatibility

Bun is fully compatible with Node.js’s child_process module:
For IPC using child_process.fork(), use process.send() and process.on("message") in the child — the same API as Bun’s native IPC.