Spawn a new process
Symbol
spawn
function spawn<Opts extends OptionsObject<Writable, Readable, Readable>>(options: Opts & { cmd: string[] }): OptionsToSubprocess<Opts>
function spawn<Opts extends OptionsObject<Writable, Readable, Readable>>(cmds: string[], options?: Opts): OptionsToSubprocess<Opts>
Spawn a new process
const {stdout} = Bun.spawn(["echo", "hello"]);
const text = await readableStreamToText(stdout);
console.log(text); // "hello\n"
Internally, this uses posix_spawn(2)
@param cmds
The command to run
The first argument will be resolved to an absolute executable path. It must be a file, not a directory.
If you explicitly set PATH
in env
, that PATH
will be used to resolve the executable instead of the default PATH
.
To check if the command exists before running it, use Bun.which(bin)
.
Referenced types
type OptionsToSubprocess<Opts extends OptionsObject> = Opts extends OptionsObject<infer In, infer Out, infer Err> ? Subprocess<Writable extends In ? 'ignore' : In, Readable extends Out ? 'pipe' : Out, Readable extends Err ? 'inherit' : Err> : Subprocess<Writable, Readable, Readable>