title: How to pass messages as well as stdout from child to parent in node.js child process module?
body: <p>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:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>I have simplified my problem below:</p>
  
  <p>parent.js is:</p>
  
  <pre><code>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);
  });
  </code></pre>
  
  <p>child.js is:</p>
  
  <pre><code>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");
  </code></pre>
  
  <p>As expected. The output is:</p>
  
  <pre><code>Msg from child
  myObj received in child:
  { a: 1 }
  Received object in parent:
  { a: 'Changed value' }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>n.stdout.on('data',function (data) {console.log(data);});
      ^
  TypeError: Cannot read property 'on' of null
  </code></pre>
  
  <p>I do not mind using any of the child-process asynchronous variations, exec, fork or spawn. Any suggestions?</p>