some_groceries
some_groceries

Reputation: 1192

nodejs: child process does not execute process.on('message')

im sending messages between parent process and child process. the parent process works as expected as it sends and receives messages from child process, but the child process is only able to send message and not receive them

app.js

import { fork } from 'child_process';

const child = fork('./script.js');

child.on('message', msg => console.log('msg from child :', msg));

child.send({ msg: 'node' }, () => console.log('parent sent msg'));

script.js

process.on('message', msg => console.log('from parent :', msg));

process.send({ msg: 'child' }, () => console.log('child sent msg'));

output in terminal

(node:7852) ExperimentalWarning: The ESM module loader is experimental.
parent sent msg
(node:9008) ExperimentalWarning: The ESM module loader is experimental.
child sent msg
msg from child : { msg: 'child' }

the callback passed to child.send is not reliable and does not indicate the message being sent, so i've read

Upvotes: 5

Views: 1079

Answers (1)

some_groceries
some_groceries

Reputation: 1192

turns out The es6 module loader is not stable and that's the reason messages are not being sent. switched to commonjs modules and it worked as expected.

i guess they prematurely removed the 'experimental' flag for running es modules. ¯_(ツ)_/¯

Upvotes: 3

Related Questions