IPC handler receives message and subprocess instance
The ipc handler function passed to Bun.spawn() receives two arguments: the message sent from the child process, and the subprocess instance (childProc) which can be used to send messages back to the child.
Child process sends IPC messages with process.send()
The child process sends messages to its parent using process.send(). It receives messages from the parent using process.on("message", handler). This is the same API used for child_process.fork() in Node.js.
Default IPC message serialization uses JSC serialize API
By default, IPC messages are serialized using the JSC serialize API, which supports everything structuredClone supports, including strings, typed arrays, and objects. This does not support transferring ownership of objects.
IPC message examples in parent process
Example showing parent process IPC usage:
const childProc = Bun.spawn(["bun", "child.ts"], {
ipc(message, childProc) {
childProc.send("Respond to child");
},
});
childProc.send("I am your father");
IPC message examples in child process
Example showing child process IPC usage:
process.send("Hello from child as string");
process.send({ message: "Hello from child as object" });
process.on("message", message => {
console.log(message);
});
Bun.spawn() with IPC for inter-process communication
Use Bun.spawn() to spawn a child process with an inter-process communication (IPC) channel. When spawning a second bun process, you can open a direct IPC channel between the two processes. The ipc option accepts a handler function that receives messages and the child process instance.
IPC with Node.js processes requires JSON serialization
To communicate with a Node.js process using IPC, set serialization: "json" in the options passed to Bun.spawn(). Use process.execPath to get the path to the currently running bun executable.
Parent process sends IPC messages with .send()
The parent process sends messages to the subprocess using the .send() method on the Subprocess instance returned by Bun.spawn(). Messages can be sent immediately after spawning or from within the ipc handler.
Bun.spawn stderr option pipe
When spawning a child process with Bun.spawn(), set the stderr option to "pipe" to read and handle stderr instead of inheriting it from the spawning process. When stderr is set to "pipe", proc.stderr is a ReadableStream.
Read stderr from child process as string
To read stderr until the child process exits, call .text() on proc.stderr, which returns a promise resolving to a string containing the error output.
Bun.spawn with stderr pipe example
Example showing how to spawn a child process and read stderr:
const proc = Bun.spawn(["echo", "hello"], {
stderr: "pipe",
});
const errors: string = await proc.stderr.text();
if (errors) {
// handle errors
}
Pipe child stdout to parent stdout example
const proc = Bun.spawn(["echo", "hello"], {
stdout: "inherit",
});
This example shows how to spawn a child process with the stdout option set to 'inherit' to directly output the child's stdout to the parent's stdout.
Bun.spawn() proc.stdout is a ReadableStream
When you spawn a child process with Bun.spawn(), the proc.stdout property is a ReadableStream of the child's stdout. You can read from it using methods like .text() to get the full output as a string.
Read child process stdout output example
const proc = Bun.spawn(["echo", "hello"]);
const output = await proc.stdout.text();
output; // => "hello\n"
This example shows how to spawn a child process and read its entire stdout output as a string using the .text() method.
Bun.spawn() stdout 'inherit' option
When spawning a child process with Bun.spawn(), you can set the stdout option to 'inherit' to pipe the child process's stdout directly to the parent's stdout instead of reading it as a stream.
Bun.spawn() configuration object
The second argument to Bun.spawn() is a configuration object. It supports the following properties: cwd (working directory), env (environment variables object), and onExit (callback function that receives proc, exitCode, signalCode, and error parameters).
Bun.spawn() example with configuration
const proc = Bun.spawn(["echo", "Hello, world!"], {
cwd: "/tmp",
env: { FOO: "bar" },
onExit(proc, exitCode, signalCode, error) {
// exit handler
},
});
This example demonstrates spawning a process with a custom working directory, environment variables, and an exit handler callback.
Bun.spawn() basic usage
Bun.spawn() spawns a child process. The first argument is an array containing the command and its arguments. Call await proc.exited to wait for the process to complete.
proc.stdout is a ReadableStream
By default, proc.stdout is a ReadableStream of the child process's stdout. It has a text() method that returns a promise resolving to the output as a string.
Bun.spawn() example with stdout
const proc = Bun.spawn(["echo", "hello"]);
const output = await proc.stdout.text();
output; // => "hello\n"
This example shows how to capture and read the stdout output from a spawned child process.