Node Streams
Streams are one of the most powerful and efficient features in Node.js.
They allow you to work with data piece by piece, rather than loading entire files or responses into memory. This makes streams ideal for handling large datasets, file operations, and network communication.
Why streams matter
Streams help you:
- process large files without memory issues
- improve performance for I/O‑heavy tasks
- handle data as it arrives (e.g., from a network request)
- build efficient pipelines for transforming data
They are a core part of Node’s design and are used internally by many APIs.
Types of streams
Node.js provides four main types of streams:
- Readable – you read data from them
- Writable – you write data to them
- Duplex – both readable and writable
- Transform – duplex streams that modify data as it passes through
Examples include file streams, HTTP request/response objects, and compression utilities.
Basic example
import fs from "node:fs";
const readable = fs.createReadStream("input.txt");
const writable = fs.createWriteStream("output.txt");
readable.pipe(writable);
console.log("File copied using streams");