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

12,619

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

Share:
12,619
Sunny
Author by

Sunny

Web designer .

Updated on June 30, 2022

Comments

  • Sunny
    Sunny almost 2 years

    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?

  • Kunok
    Kunok about 7 years
    By the way, is there any way to read these logs and process them while using {silent:true} ?
  • Benito
    Benito about 6 years
    @Kunok 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 })