Skip to main content

How does Node.js support the Web Streams API?

In Node.js, the Web Streams API is an implementation of the standard WHATWG Streams, making Node streams compatible with the ones used in browsers. This lets you write a single body of streaming code that behaves the same way in both Node.js and the browser.

When support arrived

  • Web Streams first appeared in Node.js v16.5 as an experimental feature.
  • Since Node.js v21, they're fully stable and available via the node:stream/web module.

The main Web Streams classes

Node.js implements the standard Web Streams classes:

  • ReadableStream, a stream for reading data;
  • WritableStream, a stream for writing;
  • TransformStream, a stream that reads, transforms, and outputs data.

Example:

javascript
import { ReadableStream } from 'node:stream/web'; const stream = new ReadableStream({ start(controller) { controller.enqueue('Hello, '); controller.enqueue('Web Streams!'); controller.close(); }, }); const reader = stream.getReader(); while (true) { const { value, done } = await reader.read(); if (done) break; console.log(value); }

Compatibility with Node.js streams

Node.js provides dedicated methods for converting between classic Node.js streams (stream.Readable, stream.Writable) and Web Streams:

javascript
import { Readable, Writable } from 'node:stream'; // Node.js → Web Stream const webReadable = Readable.toWeb(fs.createReadStream('input.txt')); // Web Stream → Node.js const nodeReadable = Readable.fromWeb(webReadable);

The same works for Writable and Transform streams too. This lets you mix the old and new APIs in the same codebase.

Working with fetch() and Web Streams

Since version 18, Node.js includes a built-in fetch, and its body (res.body) is a Web ReadableStream:

javascript
const res = await fetch('https://example.com/data'); const webStream = res.body; // this is a ReadableStream (a Web API)

It can be worked with using Web Streams methods, for example pipeTo():

javascript
await webStream.pipeTo(new WritableStream({ write(chunk) { console.log('Got a chunk:', chunk); }, }));

Why this matters

  • Browser compatibility, the same streaming code can now target both the frontend and the backend.
  • A modern API, built on promises, supporting pipeTo, pipeThrough, getReader(), cancel(), and it plays nicely with async/await.
  • Migration, code can move from Node.js streams to Web Streams gradually, without a full rewrite.

Differences from classic Node.js streams

FeatureNode.js StreamsWeb Streams
ModelEvent-based (on('data'), pipe())Asynchronous (pipeTo, for await...of)
Flow controlAutomatic, via backpressureVia controllers (controller.enqueue(), desiredSize)
StandardNode.js-specificWHATWG (the browser standard)
fetch() supportNoYes
Promise supportPartialFull

Summary

The Web Streams API in Node.js is a modern, standardized implementation of streams, compatible with the browser model. It's exposed through the node:stream/web module, fully supports ReadableStream, WritableStream, TransformStream, and lets you freely convert them to and from classic Node.js streams.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.