Skip to content

fork child process does not receive message event when using ES modules #34785

Closed
@dheeraj-br

Description

@dheeraj-br
  • Version: v14.8.0
  • Platform: windows 10 pro 64 bit
  • Subsystem: ES6 module

steps to reproduce the bug

im trying to have two way messaging between child process and the parent process, the parent process works as expected it sends and receives messages from child process, but the child process is only able to send message and not receive them

ive enabled ES6 modules from package.json using this key-value "type": "module"

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: 'parent' }, () => console.log('parent sent msg'));

script.js


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

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

expected behavior

parent sent msg
msg from child : { msg: 'child' }
msg from parent : { msg: 'parent' }
child sent msg

output recieved

parent sent msg
msg from child : { msg: 'child' }
child sent msg

Additional information

this issue is not present while using commonJs modules (ie: require('child_process') )

same issue closed without bug fix, nodejs/help#1383
although the workaround would be to call the child.send in the next tick like shown below, this is not homologous to the commonJs behavior

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    child_processIssues and PRs related to the child_process subsystem.esmIssues and PRs related to the ECMAScript Modules implementation.wontfixIssues that will not be fixed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions