Spawn a process (Bun.spawn()
)
Provide a command as an array of strings. The result of Bun.spawn()
is a Bun.Subprocess
object.
Bun.spawn
is a parameters object that can be used to configure the subprocess.
Input stream
By default, the input stream of the subprocess is undefined; it can be configured with thestdin
parameter.
Value | Description |
---|---|
null | Default. Provide no input to the subprocess |
"pipe" | Return a FileSink for fast incremental writing |
"inherit" | Inherit the stdin of the parent process |
Bun.file() | Read from the specified file |
TypedArray | DataView | Use a binary buffer as input |
Response | Use the response body as input |
Request | Use the request body as input |
ReadableStream | Use a readable stream as input |
Blob | Use a blob as input |
number | Read from the file with a given file descriptor |
"pipe"
option lets incrementally write to the subprocess’s input stream from the parent process.
ReadableStream
to stdin
lets you pipe data from a JavaScript ReadableStream
directly to the subprocess’s input:
Output streams
You can read results from the subprocess via thestdout
and stderr
properties. By default these are instances of ReadableStream
.
stdout/stderr
:
Value | Description |
---|---|
"pipe" | Default for stdout . Pipe the output to a ReadableStream on the returned Subprocess object |
"inherit" | Default for stderr . Inherit from the parent process |
"ignore" | Discard the output |
Bun.file() | Write to the specified file |
number | Write to the file with the given file descriptor |
Exit handling
Use theonExit
callback to listen for the process exiting or being killed.
exited
property is a Promise
that resolves when the process exits.
bun
process will not terminate until all child processes have exited. Use proc.unref()
to detach the child process from the parent.
Resource usage
You can get information about the process’s resource usage after it has exited:Using AbortSignal
You can abort a subprocess using anAbortSignal
:
Using timeout and killSignal
You can set a timeout for a subprocess to automatically terminate after a specific duration:SIGTERM
signal. You can specify a different signal with the killSignal
option:
killSignal
option also controls which signal is sent when an AbortSignal is aborted.
Using maxBuffer
For spawnSync, you can limit the maximum number of bytes of output before the process is killed:Inter-process communication (IPC)
Bun supports direct inter-process communication channel between twobun
processes. To receive messages from a spawned Bun subprocess, specify an ipc
handler.
.send()
method on the returned Subprocess
instance. A reference to the sending subprocess is also available as the second argument in the ipc
handler.
process.send()
and receive messages with process.on("message")
. This is the same API used for child_process.fork()
in Node.js.
child.ts
child.ts
serialization
option controls the underlying communication format between the two processes:
advanced
: (default) Messages are serialized using the JSCserialize
API, which supports cloning everythingstructuredClone
supports. This does not support transferring ownership of objects.json
: Messages are serialized usingJSON.stringify
andJSON.parse
, which does not support as many object types asadvanced
does.
IPC between Bun & Node.js
To use IPC between abun
process and a Node.js process, set serialization: "json"
in Bun.spawn
. This is because Node.js and Bun use different JavaScript engines with different object serialization formats.
bun-node-ipc.js
Blocking API (Bun.spawnSync()
)
Bun provides a synchronous equivalent of Bun.spawn
called Bun.spawnSync
. This is a blocking API that supports the same inputs and parameters as Bun.spawn
. It returns a SyncSubprocess
object, which differs from Subprocess
in a few ways.
- It contains a
success
property that indicates whether the process exited with a zero exit code. - The
stdout
andstderr
properties are instances ofBuffer
instead ofReadableStream
. - There is no
stdin
property. UseBun.spawn
to incrementally write to the subprocess’s input stream.
Bun.spawn
API is better for HTTP servers and apps, and Bun.spawnSync
is better for building command-line tools.
Benchmarks
⚡️ Under the hood,
Bun.spawn
and Bun.spawnSync
use
posix_spawn(3)
.spawnSync
spawns processes 60% faster than the Node.js child_process
module.
terminal
terminal
Reference
A reference of the Spawn API and types are shown below. The real types have complex generics to strongly type theSubprocess
streams with the options passed to Bun.spawn
and Bun.spawnSync
. For full details, find these types as defined bun.d.ts.
See Typescript Definitions