Pipeline for Better Error Handling
You'll learn why stream.pipeline() is the recommended way to connect streams with proper error handling and cleanup.
When working with streams, you've learned to use .pipe() to chain them together. But there's a significant problem: pipe() doesn't properly handle errors or cleanup. If a stream in the middle of your chain emits an error, the other streams may not close properly, causing memory leaks and hanging processes.
The Problem with pipe()
Using .pipe(), you'd need to manually attach error handlers to every stream in the chain. Miss one, and your error handling is incomplete. Plus, cleanup doesn't happen automatically—streams might stay open when they should close.
The Solution: stream.pipeline()
The stream.pipeline() utility (available via const { pipeline } = require('stream')) solves this elegantly. It:
- Forwards errors from any stream to a single callback
- Automatically destroys all streams if one fails
- Ensures proper cleanup when the pipeline completes or errors
const { pipeline } = require('stream');
const fs = require('fs');
const zlib = require('zlib');
pipeline(
fs.createReadStream('input.txt'),
zlib.createGzip(),
fs.createWriteStream('output.txt.gz'),
(err) => {
if (err) {
console.error('Pipeline failed:', err);
} else {
console.log('Pipeline succeeded');
}
}
);
The final argument is a callback that runs once—either with an error or on success. If any stream in the chain fails, all streams are destroyed and the error is passed to your callback.
Key Takeaway: Use stream.pipeline() instead of .pipe() for production code—it handles errors correctly and prevents resource leaks by automatically cleaning up all streams in the chain.