Node.js · Backend · Performance · JavaScript
A Node.js Streams Guide: Processing Big Files Without Running Out of Memory
Node.js stream types, error-safe flows with pipeline, backpressure, reading huge CSV and log files line by line, and streaming HTTP responses.

A service that reads a 2 GB log file with fs.readFile either crashes with an out-of-memory error or blocks the event loop for seconds. Node.js's answer to this kind of work is streams: processing data in small chunks instead of all at once. Working with test reports, device logs and export files, streams became one of the tools I rely on most.
The four stream types
| Type | Role | Example |
|---|---|---|
| Readable | Produces data | fs.createReadStream, an HTTP request body |
| Writable | Consumes data | fs.createWriteStream, an HTTP response |
| Duplex | Both directions | A TCP socket |
| Transform | Passes data through, changed | zlib.createGzip, a CSV parser |
pipeline: always use it
An a.pipe(b).pipe(c) chain doesn't propagate errors properly and may leave streams open on failure. Use pipeline from stream/promises instead:
import { createReadStream, createWriteStream } from 'node:fs';
import { createGzip } from 'node:zlib';
import { pipeline } from 'node:stream/promises';
await pipeline(
createReadStream('device.log'),
createGzip(),
createWriteStream('device.log.gz')
);
If any step fails, pipeline closes every stream and the promise rejects. Memory use stays flat regardless of file size.
What is backpressure?
If reading is faster than writing, data piles up somewhere. When a Writable's internal buffer is full, write() returns false; the right move is to pause reading until the drain event. pipeline does this for you. If you call write by hand, respect that signal, or you lose the point of streaming. The same idea shows up with WebSockets; I describe handling it with bufferedAmount in realtime systems with Node.js and WebSocket.
Processing a file line by line
import { createReadStream } from 'node:fs';
import { createInterface } from 'node:readline';
const lines = createInterface({ input: createReadStream('device.log'), crlfDelay: Infinity });
let errors = 0;
for await (const line of lines) {
if (line.includes(' E/')) errors++;
}
console.log('error lines:', errors);
Using async iterators with for await makes streams readable and backpressure-friendly.
Writing your own Transform
import { Transform } from 'node:stream';
const toJsonLines = new Transform({
objectMode: true,
transform(record, _encoding, callback) {
callback(null, JSON.stringify(record) + '\n');
}
});
objectMode lets chunks be JavaScript objects instead of bytes, which is ideal for transforming database records on their way to a file.
Streaming an HTTP response
Instead of building a large CSV export in memory and then sending it, stream it straight into the response:
app.get('/export.csv', async (req, res) => {
res.setHeader('Content-Type', 'text/csv; charset=utf-8');
res.setHeader('Content-Disposition', 'attachment; filename="runs.csv"');
await pipeline(db.streamRuns(), toCsv(), res);
});
The user gets the first byte right away and server memory stays flat. If the client disconnects, pipeline also closes the database stream.
When streams are overkill
Setting up a stream to read a few hundred kilobytes of JSON just complicates the code. The rule is simple: streams for any data that can be large or has unpredictable size, plain reads for small, bounded data.
Conclusion
Streams are the natural way to handle big data in Node.js: flat memory, an early first byte and correct error handling. With pipeline and async iterators, that power comes with readable code.


