stream.pipeline()
Pipes streams together, forwarding errors and cleaning up on failure.
Syntax
await pipeline(source, ...transforms, destination) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
source | Readable | Iterable | Yes | The first stream or iterable in the chain. |
transforms | ...Transform | No | Zero or more intermediate transform streams. |
destination | Writable | Yes | The final writable stream. |
Returns
Promise<void> — Resolves when the data has fully flowed; rejects on any stream error.
Examples
import { createReadStream, createWriteStream } from 'node:fs';
import { createGzip } from 'node:zlib';
import { pipeline } from 'node:stream/promises';
await pipeline(
createReadStream('app.log'),
createGzip(),
createWriteStream('app.log.gz'),
);
console.log('compressed');
Output
compressed
import { pipeline } from 'node:stream/promises';
try {
await pipeline(src, dest);
} catch (err) {
console.error('stream failed:', err.message);
}
Output
stream failed: ENOENT
Notes
Prefer `pipeline` over `.pipe()`: `.pipe()` does not forward errors
or destroy the source on failure, leaking file descriptors. The
promises form (`node:stream/promises`) integrates with async/await.