【发布时间】:2023-02-07 10:57:25
【问题描述】:
我正在尝试使用 NodeJS 在 shell 中执行一些命令。因此我使用node:child_process 模块。
我使用 spawn 函数以便能够将子进程的输出转发到主进程的控制台。
为了保持子进程输出的格式,我传递了选项stdio: "inherit"(如本问题所述:preserve color when executing child_process.spawn)。
但是,如果我添加此选项,子进程事件(退出、断开连接、关闭...)将不再起作用。如果我去掉该选项,我将丢失格式,但事件有效。当子进程关闭时,有没有办法保持格式并得到通知?
(相关)代码:
const { spawn } = require("node:child_process");
let child = spawn("yarn", args, {
stdio: "inherit",
shell: true,
});
child.on("close", (code) => {
console.log(`child process exited with code ${code}`);
});
【问题讨论】:
标签: node.js child-process stdio