Чтобы иметь возможность отправлять сообщения необходимо использовать fork
вместо spawn
The child_process.fork() method is a special case of child_process.spawn() used specifically to spawn new Node.js processes. Like child_process.spawn(), a ChildProcess object is returned. The returned ChildProcess will have an additional communication channel built-in that allows messages to be passed back and forth between the parent and child. See subprocess.send() for details.
Так что в итоге это будет так:
// master.js
'use strict';
const path = require('path');
const { fork } = require('child_process');
const prcs = fork(path.join(__dirname, './slave.js'));
prcs.on('message', msg => {
console.log('SLAVE is saying:', msg);
});
prcs.send('DO IT!!!');
// slave.js
'use strict';
process.on('message', msg => {
console.log('PARENT is saying:', msg);
});
setTimeout(() => {
process.send('Let me free please');
process.exit(0);
}, 1000);