Incremental data processing
Process large files or network responses incrementally without buffering the complete payload.
Node.js · Module
The node:stream module provides abstractions and utilities for processing data incrementally. It includes Readable, Writable, Duplex, and Transform stream types plus callback- and Promise-based composition APIs.
The node:stream module supplies abstractions for incremental data flow. Readable streams provide data, writable streams consume it, duplex streams support both directions, and transforms modify data while it passes through; streams also emit events and can be connected with pipeline utilities.
import { Readable, Writable, Duplex, Transform, pipeline } from 'node:stream';
const stream = require('node:stream');import { Readable, Transform, pipeline } from "node:stream";
const upper = new Transform({
transform(chunk, _encoding, callback) {
callback(null, chunk.toString().toUpperCase());
},
});
await pipeline(
Readable.from(["hello ", "streams"]),
upper,
process.stdout,
);Creates a readable stream from iterable data, transforms each chunk to uppercase, and pipelines the result to standard output.
Process large files or network responses incrementally without buffering the complete payload.
Connect a data source, transformation, and destination while propagating completion and errors.
Define custom sources, destinations, bidirectional channels, or data converters.
The four fundamental kinds are Writable, Readable, Duplex, and Transform. Writable accepts data, Readable produces data, Duplex supports both directions, and Transform is a Duplex that changes data as it passes through.
Follow-up: How would you implement a read/write stream? Implement the _read() method for the readable side and _write(), _writev(), and _final() as needed for the writable side.
Streams are EventEmitters, so readable streams can notify application code when data is available. Writable streams expose operations such as write() and end() for sending data to their destination.
Follow-up: What API can replace callback-based stream utilities? Use the stream/promises API, available through node:stream/promises or node:stream.promises, which returns Promises.
node:stream module accessed and what is it for?Import the module with require('node:stream') in CommonJS or with named imports from node:stream in ES modules. The module is primarily useful for creating new stream types and accessing stream classes and utilities.
Follow-up: Which methods are needed for a read-only custom stream? Implement _read() on a Readable; writable-only implementations use _write(), _writev(), and _final() as applicable.
ReadableWritableDuplexTransformpipelinefinishedstream/promises