Reputation: 3901
Playing around with child_process and I want to pipe spawned cp output to custom stream.
I don't understand why in first case piping doesn't works and in second does.
const cp = require('child_process');
const process = require('process');
const stream = require('stream');
var writable = new stream.Writable();
writable._write = function (data) {
console.log(data.toString());
};
var spawnedProcess = cp.spawn('ls', [], {
stdio: [process.stdin, process.stdout, process.stderr]
});
process.stdout.pipe(writable);
Outputs log into terminal but does't pipe it.
var spawnedProcess = cp.spawn('ls', [], {});
spawnedProcess.stdout.pipe(writable);
Pipes output to writable
.
Upvotes: 1
Views: 715
Reputation: 13692
The doc says process.stdout
is only a Writable stream so you can't pipe from it. It's weird it doesn't throw an Error: Cannot pipe. Not readable.
though. Also, cp.stdout
is a Readable stream so it pipes as it should.
Upvotes: 1