File processing
Copying or transforming large files without loading them fully into memory.
Node.js · Function
stream.pipeline() connects a source through optional transforms to a destination, forwarding data, errors, and cleanup. It also handles backpressure and has a promise-based API.
pipeline connects a source, optional transforms, and a destination, while coordinating data flow and cleanup. It forwards errors, handles backpressure, and can either invoke a completion callback or return a promise through node:stream/promises.
stream.pipeline(source[, ...transforms], destination, callback)
stream.pipeline(streams, callback)
pipeline(source[, ...transforms], destination[, options])| Name | Type | Description |
|---|---|---|
source | Stream | Iterable | AsyncIterable | Function | ReadableStream | Input stream, iterable, async iterable, function, or compatible readable source. |
transforms | Stream | Function | TransformStream | Zero or more intermediate transformation stages. |
destination | Stream | Function | WritableStream | Output stream, function, or compatible writable destination. |
callback | Function | Completion callback invoked when processing ends. |
Returns the destination pipeline stream for the callback API; the promise API returns a promise that fulfills on success and rejects on failure.
stream.pipeline() links streams, generators, and other supported stages while forwarding failures and managing cleanup.import { pipeline } from 'node:stream/promises';
import { Readable } from 'node:stream';
import { createWriteStream } from 'node:fs';
await pipeline(
Readable.from(['hello\n', 'world\n']),
createWriteStream('output.txt'),
);This promise-based pipeline writes generated chunks to a file and rejects if a pipeline stage fails.
Copying or transforming large files without loading them fully into memory.
Connecting readable input, transformation logic, and writable output with centralized failure handling.
Using async generators to transform chunks asynchronously.
stream.pipeline() solve beyond manually calling .pipe()?pipeline connects streams or compatible iterables and generators in sequence, forwards errors, and performs cleanup. Its callback runs when the pipeline has completed.
Follow-up: How can callback-free code use it? Import pipeline from node:stream/promises; it returns a promise that resolves on success and rejects on failure.
The function accepts a source, zero or more transforms, a destination, and a callback, or an array-like collection plus a callback. The source and stages may include streams, iterables, async iterables, functions, and Web Streams-compatible objects.
Follow-up: What does the callback receive? It receives an error when the pipeline fails; otherwise it is called without an error after completion.
Pipeline coordinates backpressure and backpressure-related errors while forwarding data between stages. This makes it suitable for long-running or large data flows without manually wiring every stage.
Follow-up: How can asynchronous transformation be added? Use an async generator as a transform stage, yielding transformed chunks from the received source.
streamstream/promisesstream.composeReadableWritableTransform