2017-06-30 26 views
2
const barStream = fs.createWriteStream('bar'); 
const foo = spawn('foo', [], { stdio: ['ignore', barStream, 'inherit'] }); 

抛出一个错误:不正确的值:WriteStream

Incorrect value for stdio stream: WriteStream

相反这样做就像foo.stdout.pipe(barStream)可能确实在某些情况下的伎俩。

但为什么WriteStream不能作为标准输出流提供?有没有办法让barStream适合stdout?

回答

3

documentation for the stdio option,重点煤矿:

<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).

所以:

const barStream = fs.createWriteStream('bar'); 

barStream.on('open',() => { 
    const foo = spawn('foo', [], { stdio: ['ignore', barStream, 'inherit'] }); 
    ⋮ 
}); 

我承认,错误消息肯定可以提供更多的帮助。

+0

清晰简单。谢谢。 – estus