Streams Basics
8 examples to get you started with Streams - 6 basic and 2 intermediate.
Search across all documentation pages
8 examples to get you started with Streams - 6 basic and 2 intermediate.
import { Readable, Writable } from 'node:stream'.Generate chunks without loading everything into memory.
import { Readable } from 'node:stream';
const readable = Readable.from(['chunk-a', 'chunk-b', 'chunk-c']);
for await (const chunk of readable) {
console.log(chunk);
}Readable.from wraps iterables and async iterables.for await...of consumes until null end.objectMode: true.Related: Readable & Writable Patterns - modes
Connect producer to consumer - classic Unix-style piping.
import { createWriteStream } from 'node:fs';
import { Readable } from 'node:stream';
Readable.from(['line1\n', 'line2\n']).pipe(createWriteStream('out.txt'));pipe wires data to write and handles end.pipeline in production.pipe does not forward errors automatically on older patterns.Related: stream/promises.pipeline - safe piping
Stream large files from disk.
import { createReadStream } from 'node:fs';
const stream = createReadStream('package.json', { encoding: 'utf8', highWaterMark: 64 * 1024 });
stream.on('data', (chunk) => console.log('chunk', chunk.length));
stream.on('end', () => console.log('done'));highWaterMark controls internal buffer size per stream.pipeline to HTTP response for downloads.ServerResponse is a Writable stream.
import { createServer } from 'node:http';
import { createReadStream } from 'node:fs';
createServer((req, res) => {
createReadStream('package.json').pipe(res);
}).listen(3000);writeHead first.pipeline handles cleanup.Change data as it passes through.
import { Transform } from 'node:stream';
const upper = new Transform({
transform(chunk, _enc, cb) {
cb(null, String(chunk).toUpperCase());
},
});
Readable.from(['hello']).pipe(upper).pipe(process.stdout);cb(err, data) signals chunk processed or error._flush runs at end for trailing output.Related: Transform & Duplex - parsing pipelines
Stream JavaScript objects instead of bytes.
import { Transform } from 'node:stream';
const parseLines = new Transform({
objectMode: true,
transform(chunk, _enc, cb) {
const lines = String(chunk).split('\n').filter(Boolean);
for (const line of lines) this.push({ line });
cb();
},
});highWaterMark counts objects in object mode, not bytes.data events.Error propagation and cleanup in one call.
import { pipeline } from 'node:stream/promises';
import { createReadStream, createWriteStream } from 'node:fs';
import { createGzip } from 'node:zlib';
await pipeline(
createReadStream('access.log'),
createGzip(),
createWriteStream('access.log.gz'),
);pipeline destroys streams on error or early close.try/catch in async handlers..pipe().pipe() chains.Related: stream/promises.pipeline - deep dive
Writable signals when it cannot accept more data.
import { Readable, Writable } from 'node:stream';
const slow = new Writable({
write(chunk, _enc, cb) {
setTimeout(() => cb(), 100);
},
});
const fast = Readable.from(['a', 'b', 'c', 'd']);
fast.pipe(slow); // pipe handles pause/resume automaticallywrite returns false, pause readable until drain.read loops must check writable.write return value.Stack versions: This page was written for Node.js 24.18.0 (Active LTS), npm 10+, TypeScript 5.6+, Express 5, Fastify 5, and NestJS 11.
Reviewed by Chris St. John·Last updated Jul 16, 2026