Piping and Backpressure
Backpressure occurs when a writable destination cannot consume data as quickly as a readable source produces it. Node.js streams provide mechanisms for controlling this flow.
The pipe() method connects streams and handles important flow-control behavior automatically. Modern applications can also use pipeline() from node:stream/promises to compose streams while handling errors more safely.
pipeline() provides cleaner error handling and lifecycle management when connecting multiple streams.
import { createReadStream, createWriteStream } from 'node:fs';
import { pipeline } from 'node:stream/promises';
await pipeline(
createReadStream('input.txt'),
createWriteStream('output.txt')
);
console.log('Copy completed');pipeline() connects the streams and provides Promise-based completion and error propagation.
Backpressure prevents fast producers from overwhelming slow consumers.
Use
pipeline()when composing multiple streams.Stream errors must be handled explicitly.
What does backpressure protect against?