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

Sunny picture Sunny · Nov 2, 2015 · Viewed 9.9k times · Source

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?

Answer

Neil picture Neil · Mar 3, 2016

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 });