使用 ipc 的 Windows 上的节点 child_process.spawn 错误文件描述符

ond*_*akr 5 windows ipc spawn child-process node.js

我正在尝试通过 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)

这种情况仅发生在特殊配置中:

  1. 在窗户上
  2. 带“ipc”选项
  3. js中的spawned命令
    会失败:another.jsnpm
    不会失败:node

有一个已关闭的问题,没有多大用处。

shell: true根据这篇文章,我需要跨平台兼容性。

这个问题似乎也相关,但读完后我并没有变得更聪明。

节点 v12.18.3

感谢帮助。

Cha*_*oza 2

你不能将“ipc”与npm一起使用,因为npm“二进制”是一个shell脚本,而不是一个节点进程(请参阅https://github.com/npm/cli/tree/latest/bin

文件

不支持以 process.send() 以外的任何方式访问 IPC 通道 fd 或将 IPC 通道与非 Node.js 实例的子进程一起使用。

对于another.js,要么使用fork(将始终有效),要么spawn('node', 'another.js')如果您不使用节点垫片(如 nodist),则应该有效