2017-06-30 28 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