← Back to Node.js Course | Chapter 2: Core Modules | Lesson 6 of 7

stream basics

Streams let you process big data a piece at a time instead of loading it all into memory.

In this page:

  1. stream basics
Syntax
javascript
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

javascript
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
  1. Reading a huge file with readFile and running out of memory
  2. Ignoring backpressure
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.