Suggest an editImprove this articleRefine the answer for “How does Node.js support the Web Streams API?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)Node.js implements the standard WHATWG Web Streams API via the `node:stream/web` module (`ReadableStream`, `WritableStream`, `TransformStream`), and provides `Readable.toWeb()` / `Readable.fromWeb()` to convert in both directions between classic Node.js streams and Web Streams. **Key point:** the built-in `fetch()`'s body (`res.body`) is already a web-standard `ReadableStream`, so the same streaming code works in both the browser and Node.js.Shown above the full answer for quick recall.Answer (EN)ImageIn 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 | Feature | Node.js Streams | Web Streams | |---|---|---| | Model | Event-based (`on('data')`, `pipe()`) | Asynchronous (`pipeTo`, `for await...of`) | | Flow control | Automatic, via backpressure | Via controllers (`controller.enqueue()`, `desiredSize`) | | Standard | Node.js-specific | WHATWG (the browser standard) | | `fetch()` support | No | Yes | | Promise support | Partial | Full | ## 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.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.