method
Spawn.SpawnOptions.ipc
When specified, Bun opens an IPC channel to the subprocess. The passed callback is called for incoming messages, and subprocess.send can send messages to the subprocess. Messages are serialized using the JSC serialize API, which allows the same types that postMessage/structuredClone supports.
The subprocess can send and receive messages with process.send and process.on("message"), respectively. This is the same API that Node.js exposes when child_process.fork() is used.
This is only compatible with processes that are other bun instances.
The Subprocess that received the message
Referenced types
interface Subprocess<In extends SpawnOptions.Writable = SpawnOptions.Writable, Out extends SpawnOptions.Readable = SpawnOptions.Readable, Err extends SpawnOptions.Readable = SpawnOptions.Readable>
A process created by Bun.spawn.
The 3 optional type parameters correspond to the stdio array from the options object. Instead of specifying them, use one of these utility types:
- ReadableSubprocess (any, pipe, pipe)
- WritableSubprocess (pipe, any, any)
- PipedSubprocess (pipe, pipe, pipe)
- NullSubprocess (ignore, ignore, ignore)
- readonly exitCode: null | number
Synchronously get the exit code of the process
nullif the process hasn't exited yet - readonly exited: Promise<number>
The exit code of the process
The promise resolves when the process exits
- readonly pid: number
The process ID of the child process
const { pid } = Bun.spawn({ cmd: ["echo", "hello"] }); console.log(pid); // 1234 - readonly readable: ReadableToIO<Out>
The same value as Subprocess.stdout
Exists for compatibility with ReadableStream.pipeThrough
- readonly signalCode: null | Signals
Synchronously get the signal code of the process
nullif the process never sent a signal codeTo receive signal code changes, use the
onExitcallback.If the signal code is unknown, this is the original signal code number, but that case should never happen in practice.
- readonly stdio: [null, null, null, ...null | number[]]
Extra file descriptors passed to the
stdiooption.Entries beyond index 2 are
numberfor"pipe"and"socket-fd"slots and, on POSIX, for slots where a raw file descriptor was supplied (the same fd is returned). On POSIX, reading this property transfers ownership of any"pipe"fds to the caller, who is then responsible for closing them; the subprocess will not close them."socket-fd"and raw-fd slots are likewise caller-owned. Other slots — including raw fds on Windows — arenull. - readonly terminal: undefined | Terminal
The terminal attached to this subprocess, if spawned with the
terminaloption.undefinedif no terminal was attached.When a terminal is attached,
stdin,stdout, andstderrreturnnull. Useterminal.write()and thedatacallback instead.const proc = Bun.spawn(["bash"], { terminal: { data: (term, data) => console.log(data.toString()) }, }); proc.terminal?.write("echo hello\n"); Disconnect the IPC channel to the subprocess. This is only supported if the subprocess was created with the
ipcoption.- @param exitCode
Exit code or signal to send to the process
Tell Bun to wait for this process to exit after you already called
unref().By default, Bun waits for all subprocesses to exit before shutting down
Get the resource usage of the process, such as max RSS and CPU time
Returns
undefineduntil the process has exited- send(message: any): void;
Send a message to the subprocess. This is only supported if the subprocess was created with the
ipcoption, and is another instance ofbun.Messages are serialized using the JSC serialize API, which allows for the same types that
postMessage/structuredClonesupports. Tell Bun not to wait for this process to exit before shutting down.
By default, Bun waits for all subprocesses to exit before shutting down.