node.js - nodejs child_proces maxbuffer



Nodejs的child_process生成自定義的stdio (1)

它似乎是一個bug: https : customStream當它傳遞給spawn()時, customStream似乎還沒有準備好。 您可以輕鬆地解決這個問題:

const cp = require('child_process');
const stream = require('stream');

// use a Writable stream
var customStream = new stream.Writable();
customStream._write = function (data) {
    console.log(data.toString());
};

// 'pipe' option will keep the original cp.stdout
// 'inherit' will use the parent process stdio
var child = cp.spawn('ls', [], {
    stdio: [null, 'pipe', 'inherit'] 
});

// pipe to your stream
child.stdout.pipe(customStream);

https://ffff65535.com

我想使用自定義流來處理child_process.spawn stdio。

例如

const cp = require('child_process');
const process = require('process');
const stream = require('stream');

var customStream = new stream.Stream();
customStream.on('data', function (chunk) {
    console.log(chunk);
});

cp.spawn('ls', [], {
    stdio: [null, customStream, process.stderr]
});

我得到錯誤Incorrect value for stdio stream

有關於child_process.spawn的文檔https://nodejs.org/api/child_process.html#child_process_options_stdio 。 它說stdio選項,它可以採取Stream對象

流對象 - 將引用tty,文件,套接字或管道的可讀或可寫的流與子進程共享。

我想我錯過了這個“指”部分。