我正在尝试通过 child_process spawn 在带有 ipc 选项的节点中生成命令。
我所说的:
const {spawn} = require('child_process');
const cmd = spawn('npm', ['-v'], {
shell: true,
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
});
cmd.on('message', (msg) => console.log(msg));
Run Code Online (Sandbox Code Playgroud)
我得到什么:
child_process.js:122
p.open(fd);
Error: EBADF: bad file descriptor, uv_pipe_open
Child (child_process.js:122:5)
at setupChildProcessIpcChannel (internal/bootstrap/pre_execution.js:329:30)
at prepareMainThreadExecution (internal/bootstrap/pre_execution.js:54:3)
at internal/main/run_main_module.js:7:1 {
errno: -4083,
code: 'EBADF',
syscall: 'uv_pipe_open'
}
Run Code Online (Sandbox Code Playgroud)
这种情况仅发生在特殊配置中:
another.js,npmnode有一个已关闭的问题,没有多大用处。
shell: true根据这篇文章,我需要跨平台兼容性。
这个问题似乎也相关,但读完后我并没有变得更聪明。
节点 v12.18.3
感谢帮助。