NodeJS spawn stdout string format

只谈情不闲聊 提交于 2019-12-04 01:19:26

Streams are buffered and emit data events whenever they please (so to speak), not on strict boundaries like lines of text.

But you can use the readline module to parse the buffers into lines for you:

var child_process = require('child_process');
var readline      = require('readline');
var proc          = child_process.spawn(...);

readline.createInterface({
  input     : proc.stdout,
  terminal  : false
}).on('line', function(line) {
  console.log(line);
});

There are 3 solutions which come to mind:

// solution #1
process.stdout.write(data);

// solution #2
console.log(data.toString().replace(/[\n\r]/g, ""));

// solution #3
var child_process = require('child_process');
var readline = require('readline');
var proc = child_process.spawn(...);
readline.createInterface({
  input: proc.stdout,
  terminal: false
}).on('line', function(line) {
  console.log(line);
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!