Streams Basics
8 examples to get you started with Streams - 6 basic and 2 intermediate.
Prerequisites
- Node.js 24.18.0,
import { Readable, Writable } from 'node:stream'. - Read Buffers Basics for chunk handling.
Basic Examples
1. Readable from Iterator
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.fromwraps iterables and async iterables.for await...ofconsumes untilnullend.- Default is byte/string mode unless
objectMode: true.
Related: Readable & Writable Patterns - modes
2. Pipe to Writable
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'));pipewiresdatatowriteand handlesend.- Errors on either side need explicit handlers - prefer
pipelinein production. pipedoes not forward errors automatically on older patterns.
Related: stream/promises.pipeline - safe piping
3. fs.createReadStream
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'));highWaterMarkcontrols internal buffer size per stream.- Encoding produces strings; omit for raw Buffers.
- Prefer
pipelineto HTTP response for downloads.
4. HTTP Response as Writable
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);- Headers should be set before piping or via
writeHeadfirst. - Client disconnect should destroy read stream -
pipelinehandles cleanup. - See Streaming HTTP Responses for proxies.
5. Transform with map (simple)
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);- Transform is both readable and writable - sits in middle of pipeline.
- Callback
cb(err, data)signals chunk processed or error. _flushruns at end for trailing output.
Related: Transform & Duplex - parsing pipelines
6. Object Mode Stream
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();
},
});highWaterMarkcounts objects in object mode, not bytes.- Consumers must expect objects in
dataevents. - Useful for CSV row parsing and log event pipelines.
Intermediate Examples
7. pipeline with Promises
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'),
);pipelinedestroys streams on error or early close.- Returns a Promise - use
try/catchin async handlers. - Preferred over manual
.pipe().pipe()chains.
Related: stream/promises.pipeline - deep dive
8. Backpressure with pause/resume
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 automatically- When
writereturns false, pause readable untildrain. - Manual
readloops must checkwritable.writereturn value. - See Backpressure for production tuning.
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.