How to pass messages as well as stdout from child to parent in node.js child process module?
Asked Answered
B

2

23

I am having a problem with child-process module, specifically with child.spawn and child.fork. I am relying on the documentation of child_process.fork, which says:

This is a special case of the child_process.spawn() functionality for spawning Node.js processes. In addition to having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in. See child.send(message, [sendHandle]) for details.

I have simplified my problem below:

parent.js is:

var cp = require('child_process');
var n = cp.fork('./child.js');
n.send({a:1});
//n.stdout.on('data',function (data) {console.log(data);});
n.on('message', function(m) {
  console.log("Received object in parent:");
  console.log( m);
});

child.js is:

process.on('message', function(myObj) {
  console.log('myObj received in child:');
  console.log(myObj);
  myObj.a="Changed value";
  process.send(myObj);
});
process.stdout.write("Msg from child");

As expected. The output is:

Msg from child
myObj received in child:
{ a: 1 }
Received object in parent:
{ a: 'Changed value' }

I want it to work with the commented line in parent.js uncommented. In other words, I want to catch the stdout in the child process in the n.stdout.on('data'... statement in the parent process. If I uncomment it, I get an error:

n.stdout.on('data',function (data) {console.log(data);});
    ^
TypeError: Cannot read property 'on' of null

I do not mind using any of the child-process asynchronous variations, exec, fork or spawn. Any suggestions?

Baur answered 2/11, 2015 at 13:26 Comment(1)
can someone please explain - why to prefer message listener instead of data while streaming...data?Ledaledah
U
49

You need to set the silent property on the options object when you pass it in to fork() in order for the stdin, stdout and stderr to get piped back to the parent process.

e.g. var n = cp.fork('./child.js', [], { silent: true });

Unemployed answered 3/3, 2016 at 21:15 Comment(2)
By the way, is there any way to read these logs and process them while using {silent:true} ?Cannelloni
@Cannelloni From what I just tried, you can then use n.stdout.on('data', (data) => { // do something with data }) (which you could not do, had you not used { silent: true })Feathering
Z
-3
spawn('stdbuf', ['-i0', '-o0', '-e0', "./test-script" ]);
Zoology answered 11/6, 2019 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.