【发布时间】:2022-02-16 19:11:22
【问题描述】:
当我以 commonJs 语法从 parent.js 向 child.js 发送消息时,它可以工作。在 parent.js 我有
//parent.js
const cp = require('child_process');
let child = cp.fork('./child.js');
child.on('message', (message) =>{
console.log('Parent got message: '+message);
});
// parent sends a message
child.send('Parent sends message');
在 child.js 中我有:
// child.js
process.on('message', (m) => {
console.log('child got message:', m);
process.send('child sends message');
});
一切正常,在控制台中我得到了:
child got message: Parent sends message
Parent got message: child sends message
但是当我使用 ES6 导入语法时它停止工作:
import * as cp from 'child_process';
我做错了什么,还是这是一个 nodejs 错误?
我的节点版本是16.13.2 在不工作下,我的意思是终端中的光标在闪烁,但我没有收到任何消息,也没有收到任何错误。
【问题讨论】:
标签: node.js child-process