Node.js Readable Stream to Web ReadableStream Converter
The `readable-stream-node-to-web` package offers a utility to convert a Node.js `Readable` stream into a WHATWG `ReadableStream`, commonly known as a web stream. This bridge allows developers to use Node.js stream-producing modules within web-native contexts such as Service Workers or modern browser environments, where web streams are the standard. The current and last published stable version is 1.0.1, released approximately nine years ago. Due to its age and the subsequent introduction of native stream conversion utilities directly within Node.js (e.g., `stream.Readable.toWeb()` since Node.js v17), this package is largely superseded. Its development is effectively halted, and users are encouraged to explore native Node.js APIs or more actively maintained alternatives for stream interoperability. The module was written in ES5 and was compatible with bundlers like Browserify and Webpack without needing transpilation.
Common errors
-
TypeError: require(...) is not a function
cause Attempting to use `require('readable-stream-node-to-web')` in a way that expects a named export, or in a pure ESM context without proper interop.fixEnsure you are using `const nodeToWebStream = require('readable-stream-node-to-web');` for CommonJS. If in an ESM file, bundlers might support `import nodeToWebStream from 'readable-stream-node-to-web';` but direct Node.js ESM might need careful handling or a switch to native APIs. -
Property 'toWeb' does not exist on type 'Readable'
cause Attempting to use `nodeStream.toWeb()` on a Node.js `Readable` stream in an older Node.js version (<v17) or without TypeScript type declarations for `toWeb`.fixEnsure Node.js is v17 or newer for native `.toWeb()` support. If using an older Node.js, you must use this `readable-stream-node-to-web` package (or a similar alternative) for conversion. For TypeScript, ensure your `tsconfig.json` targets a Node.js version that includes these types or add a custom declaration file.
Warnings
- deprecated This package has not been updated in approximately nine years (last published August 2017). It is no longer actively maintained and is considered superseded.
- gotcha The package is a CommonJS module. While bundlers typically handle interop, direct usage in pure ESM Node.js environments might require specific loader configurations or the `require` syntax.
- gotcha Node.js v17 and later include built-in `stream.Readable.toWeb()` and `stream.Writable.fromWeb()` methods, offering a native and often more performant way to convert between Node.js and Web Streams. Using this package introduces an unnecessary dependency for newer Node.js versions.
Install
-
npm install readable-stream-node-to-web -
yarn add readable-stream-node-to-web -
pnpm add readable-stream-node-to-web
Imports
- nodeToWebStream
import { nodeToWebStream } from 'readable-stream-node-to-web';import nodeToWebStream from 'readable-stream-node-to-web';
- nodeToWebStream
const nodeToWebStream = require('readable-stream-node-to-web');
Quickstart
import { Readable } from 'node:stream';
import nodeToWebStream from 'readable-stream-node-to-web';
// Create a mock Node.js Readable stream
class MockNodeReadable extends Readable {
constructor(dataArray) {
super();
this.data = [...dataArray];
}
_read() {
if (this.data.length) {
this.push(this.data.shift());
} else {
this.push(null);
}
}
}
const nodeStream = new MockNodeReadable(['Hello', ' ', 'World', '!']);
const webStream = nodeToWebStream(nodeStream);
// Consume the web stream
async function consumeWebStream() {
const reader = webStream.getReader();
let result = '';
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
result += new TextDecoder().decode(value);
}
console.log('Consumed Web Stream:', result);
}
consumeWebStream();