使用 Bun.spawn()
時,子程序會繼承產生程序的 stderr
。如果你希望讀取和處理 stderr
,請將 stderr
選項設定為 "pipe"
。
const proc = Bun.spawn(["echo", "hello"], {
stderr: "pipe",
});
proc.stderr; // => ReadableStream
若要讀取 stderr
直到子程序結束,請使用 Bun.readableStreamToText()
便利函式。
const proc = Bun.spawn(["echo", "hello"], {
stderr: "pipe",
});
const errors: string = await Bun.readableStreamToText(proc.stderr);
if (errors) {
// handle errors
}
請參閱 文件 > API > 子程序 以取得完整文件。