How to read from child_process line by line in Node.js?

I am trying to make a Node.js script to analyze disk usage. I use du for this, but it's hard for me to figure out how to read the output of the child process line by line. Here is what I have tried so far:

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

du.stdout.on('data' just reads the pieces of data, and while readline should apparently separate its input by line, I get the exact data instead (du.stdout returns a buffer, but calls .toString() it gives the same data as mine with linereader ).

+6
source share
1 answer

In the current stable version (0.6.14) Readline does not work in Node.js. We had the same problem:

fooobar.com/questions/346765 / ...

However, there is a real quick snippet of code from TooTallNate that fixes this problem for you: https://gist.github.com/1785026

There is a migration request to fix this in later versions, and it should be in version 0.7.8.

+2
source

Source: https://habr.com/ru/post/913629/


All Articles