const barStream = fs.createWriteStream('bar');
const foo = spawn('foo', [], { stdio: ['ignore', barStream, 'inherit'] });
throws an error:
Incorrect value for stdio stream: WriteStream
Doing this in reverse like foo.stdout.pipe(barStream)
likely does the trick in some cases.
But why exactly WriteStream
cannot be supplied as stdout stream? Is there a way to make barStream
suitable for stdout?
const barStream = fs.createWriteStream('bar');
const foo = spawn('foo', [], { stdio: ['ignore', barStream, 'inherit'] });
throws an error:
Incorrect value for stdio stream: WriteStream
Doing this in reverse like foo.stdout.pipe(barStream)
likely does the trick in some cases.
But why exactly WriteStream
cannot be supplied as stdout stream? Is there a way to make barStream
suitable for stdout?
1 Answer
Reset to default 10From the documentation for the stdio
option, emphasis mine:
<Stream>
object - Share a readable or writable stream that refers to a tty, file, socket, or a pipe with the child process. The stream's underlying file descriptor is duplicated in the child process to the fd that corresponds to the index in the stdio array. Note that the stream must have an underlying descriptor (file streams do not until the 'open' event has occurred).
So:
const barStream = fs.createWriteStream('bar');
barStream.on('open', () => {
const foo = spawn('foo', [], { stdio: ['ignore', barStream, 'inherit'] });
⋮
});
I’ll admit that error message could definitely be more helpful.