stream basics
Streams let you process big data a piece at a time instead of loading it all into memory.
In this page:
Syntax
const fs = require('fs');
const readable = fs.createReadStream('input');
const writable = fs.createWriteStream('output');
readable.pipe(writable);
stream basics
There are four kinds of streams: readable, writable, duplex and transform. You connect them with pipe or the stream.pipeline function, which also handles errors. Streaming is how Node copies huge files with tiny memory.
Note:
Use stream.pipeline instead of pipe when you need reliable error handling.
Example: stream basics
const { Readable, Transform, Writable, pipeline } = require("stream");
const upper = new Transform({
transform(chunk, enc, cb) { cb(null, chunk.toString().toUpperCase()); },
});
const out = [];
const sink = new Writable({ write(chunk, enc, cb) { out.push(chunk.toString()); cb(); } });
pipeline(Readable.from(["node ", "streams"]), upper, sink, (err) => {
console.log(err ? "failed" : "result: " + out.join(""));
});
// Output:
// result: NODE STREAMS
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
Common Mistakes
- Reading a huge file with readFile and running out of memory
- Ignoring backpressure
- Forgetting to handle stream errors
Chapter Summary
- Streams process data in chunks
- Four types: readable, writable, duplex, transform
- pipe or pipeline connects them
- Saves memory for large data
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: