Search code examples
node.jsgulpnode.js-stream

Merging writable node streams into one stream


I have an array of writable streams in a gulp task. I would like to merge them and return one stream. I am using the merge-stream node module to do this. I get the following error though.

[14:22:56] Error: Cannot pipe, not readable
    at WriteStream.Writable.pipe (_stream_writable.js:161:22)
    at add (C:\workspace\mediafly-viewer\desktop\node_modules\merge-stream\index.js:28:12)
    at Array.forEach (native)

Is merging writable streams supported? If so, how do I go about doing this?

Here is what I tried with promises.

const promises = streams.map(s => new Promise((resolve, reject) => s.on('finish', resolve).on('error', reject)));

return Promise.all(promises);

Solution

  • The problem is that merge-stream treats each stream as a Transform stream, i.e. it tries to call pipe on the writable stream, but writable streams do not have pipe method, only write. What you can do to fix this is to either use Transform streams for your writes, or do as you've done by using Promise to wait for the finish event.