← Back to Node.js, page 1

nodejs-0007 · Easy fresher

What are the four types of streams in Node.js, and why use a stream instead of reading the whole file?

Node has Readable, Writable, Duplex and Transform streams. A stream processes data in small chunks, so memory stays low and work can start before all data has arrived. Reading a whole file with readFile needs memory for the full file and waits until all of it is loaded.

Full answer

A stream is an object that moves data piece by piece. The four types:

  • Readable: a source you read from, such as fs.createReadStream or an incoming HTTP request.
  • Writable: a destination you write to, such as fs.createWriteStream or an HTTP response.
  • Duplex: both readable and writable, with two independent sides. A TCP socket is the usual example.
  • Transform: a duplex stream whose output is computed from its input, such as zlib.createGzip or a stream that changes text to upper case.

Why streams? Suppose a 2 GB log file must be compressed and sent to a client. With fs.readFile the process needs 2 GB of memory and the client gets nothing until the whole file is read. With streams, a 64 KB chunk is read, compressed and sent, then the next chunk. Memory stays near 64 KB, the first bytes arrive quickly, and slow clients are handled through backpressure.

Streams are also a design tool. You can connect small stages (read, decrypt, unzip, parse, save) with stream.pipeline, and each stage stays simple.

For small files, readFile is simpler and perfectly fine. Use streams when data is large, unbounded (uploads, logs, sockets) or arrives over time.

javascript
const { Readable, Writable, Transform, pipeline } = require('node:stream');

// Readable: three lines of marks.
const source = Readable.from(['maths: 78\n', 'physics: 91\n', 'chemistry: 85\n']);

// Transform: change every chunk to upper case.
const upper = new Transform({
  transform(chunk, encoding, callback) {
    callback(null, chunk.toString().toUpperCase());
  },
});

// Writable: collect what arrives.
const received = [];
const sink = new Writable({
  write(chunk, encoding, callback) {
    received.push(chunk.toString());
    callback();
  },
});

pipeline(source, upper, sink, (err) => {
  console.log(err ? 'failed: ' + err.message : 'pipeline finished');
  console.log(received.join('').trim());
});

A readable, a transform and a writable are joined with pipeline, which also handles errors and cleanup. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): pipeline finished MATHS: 78 PHYSICS: 91 CHEMISTRY: 85

Key points

  • Readable, Writable, Duplex and Transform are the four types.
  • Streams process chunks, so memory use stays flat for big data.
  • Output can start before input ends, which lowers latency.
  • Use readFile for small files and streams for large or unbounded data.

Common mistakes

  • Treating a Duplex and a Transform as the same thing. A Transform's output is derived from its input.
  • Using readFile for very large files or for uploads.
  • Using .pipe() chains without error handlers.
  • Forgetting that stream chunks are Buffers unless an encoding or object mode is set.

Follow-ups

  • What is object mode?
  • How does a stream decide how big each chunk is?
  • Why can an HTTP request be a stream and a response be a stream?

Tips

  • Give one example of each type from the core modules.
  • State both benefits of streaming: low memory and low latency.
  • Mention pipeline early, because it shows you know how to build stages safely.