Node.js Stream.pipeline() Method
Last Updated :
28 Apr, 2025
The stream.pipeline() method is a module method that is used to the pipe by linking the streams passing on errors and accurately cleaning up and providing a callback function when the pipeline is done.
Syntax:
stream.pipeline(...streams, callback)
Parameters: This method accepts two parameters as mentioned above and described below.
- ...streams: These are two or more streams that are to be piped together.
- callback: This function is called when the pipeline is fully done and it shows an 'error' if the pipeline is not accomplished.
Return Value: It returns a cleanup function.
The below examples illustrate the use of the stream.pipeline() method in Node.js:
Example 1:
JavaScript
// Node.js program to demonstrate the
// stream.pipeline() method
// Including fs and zlib module
const fs = require('fs');
const zlib = require('zlib');
// Constructing finished from stream
const { pipeline } = require('stream');
// Constructing promisify from
// util
const { promisify } = require('util');
// Defining pipelineAsync method
const pipelineAsync = promisify(pipeline);
// Constructing readable stream
const readable = fs.createReadStream("input.text");
// Constructing writable stream
const writable = fs.createWriteStream("output.text");
// Creating transform stream
const transform = zlib.createGzip();
// Async function
(async function run() {
try {
// pipelining three streams
await pipelineAsync(
readable,
transform,
writable
);
console.log("pipeline accomplished.");
}
// Shows error
catch (err) {
console.error('pipeline failed with error:', err);
}
})();
Output:
Promise { }
pipeline accomplished.
Example 2:
javascript
// Node.js program to demonstrate the
// stream.pipeline() method
// Including fs and zlib module
const fs = require('fs');
const zlib = require('zlib');
// Constructing finished from stream
const { pipeline } = require('stream');
// Constructing promisify from
// util
const { promisify } = require('util');
// Defining pipelineAsync method
const pipelineAsync = promisify(pipeline);
// Constructing readable stream
const readable = fs.createReadStream("input.text");
// Constructing writable stream
const writable = fs.createWriteStream("output.text");
// Creating transform stream
const transform = zlib.createGzip();
// Async function
(async function run() {
try {
// pipelining three streams
await pipelineAsync(
readable,
writable,
transform
);
console.log("pipeline accomplished.");
}
// Shows error
catch (err) {
console.error('pipeline failed with error:', err);
}
})();
Output: Here, the order of streams is not proper while piping so an error occurs.
Promise { }
pipeline failed with error: Error [ERR_STREAM_CANNOT_PIPE]: Cannot pipe, not readable
at WriteStream.Writable.pipe (_stream_writable.js:243:24)
at pipe (internal/streams/pipeline.js:57:15)
at Array.reduce ()
at pipeline (internal/streams/pipeline.js:88:18)
at Promise (internal/util.js:274:30)
at new Promise ()
at pipeline (internal/util.js:273:12)
at run (/home/runner/ThirstyTimelyKey/index.js:33:11)
at /home/runner/ThirstyTimelyKey/index.js:45:5
at Script.runInContext (vm.js:133:20)
Reference: https://nodejs.org/api/stream.html#stream_stream_pipeline_streams_callback.
Explore
Node.js Tutorial
3 min read
Introduction & Installation
Node.js Modules , Buffer & Streams
Node.js Asynchronous Programming
Node.js NPM
Node.js Deployments & Communication
Resources & Tools