title: How to read from a child_process line by line in Node.js?
body: <p>I am trying to make a Node.js script to analyse disk usage. For this, I shell out to <code>du</code>, but I am having trouble figuring out how to read the output from the child process line by line. Here's what I've tried so far:</p>
  
  <pre><code>var spawn = require("child_process").spawn,
      rl = require('readline'),
      du = spawn('du', ['/home']);
      linereader = rl.createInterface(du.stdout, du.stdin);
  
  // Read line by line.
  //du.stdout.on('data', function (data) {
  linereader.on('line', function (data) {
    console.log(data);
  });
  </code></pre>
  
  <p><code>du.stdout.on('data'</code> just reads chunks of data, and while <code>readline</code> should supposedly split its input by line, it doesn't, instead I get the exact same data (du.stdout returns a buffer, but calling <code>.toString()</code> on it gives me the same data I got with <code>linereader</code>).</p>