Node.js Stream API for Browsers (Browserify)
stream-browserify is a module that provides the Node.js `stream` API for browser environments. It serves as a polyfill, allowing npm packages designed for Node.js streams to function correctly within a browser context when bundled. The current stable version is 3.0.0, released after a period of using `readable-stream` v2, and now fully leverages `readable-stream` v3. This package is primarily intended for use with bundlers like Browserify, which automatically substitute the native Node.js `stream` module with `stream-browserify` during the bundling process. Its key differentiator is providing a consistent, spec-compliant stream implementation that bridges the gap between Node.js module expectations and browser limitations, ensuring broad compatibility for legacy and modern stream APIs in the browser.
Common errors
-
TypeError: stream.Readable is not a constructor
cause This typically occurs in a browser environment where the native Node.js 'stream' module is not available or has not been correctly polyfilled by a bundler, or if an old CJS `require` is used in an incompatible ESM context.fixEnsure your project is bundled with a tool like Browserify that provides `stream-browserify` as a shim for `require('stream')`. If directly importing, use `import { Readable } from 'stream-browserify';`. Verify bundler configuration. -
Cannot find module 'stream'
cause This error means that `require('stream')` or `import 'stream'` could not be resolved, most commonly in a browser environment without an appropriate polyfill or bundler configuration.fixIf using Browserify, ensure `stream-browserify` is correctly integrated by the bundler. If not using a bundler or needing a global stream polyfill, you might need to manually configure aliasing or ensure `stream-browserify` is included in your build process.
Warnings
- breaking Version 3.0.0 upgrades to `readable-stream` v3. This introduces several breaking changes inherited from `readable-stream` itself, aligning with Node.js 10+ stream APIs. Developers should consult the `readable-stream` v3 release notes for detailed information.
- gotcha Direct installation of `stream-browserify` is often unnecessary. Bundlers like Browserify automatically include and alias this module when `require('stream')` is encountered in browser environments.
- gotcha Mixing different versions of `readable-stream` (or `stream-browserify` which depends on it) within a single application can lead to unpredictable behavior and compatibility issues, especially when libraries expect specific stream API behaviors.
Install
-
npm install stream-browserify -
yarn add stream-browserify -
pnpm add stream-browserify
Imports
- Readable
const Readable = require('stream-browserify').Readable;import { Readable } from 'stream-browserify'; - Writable
import Writable from 'stream-browserify/writable';
import { Writable } from 'stream-browserify'; - Transform
const stream = require('stream-browserify'); const Transform = stream.Transform;import { Transform } from 'stream-browserify';
Quickstart
import { Readable, Writable } from 'stream-browserify';
class MyReadable extends Readable {
constructor(options) {
super(options);
this.index = 0;
}
_read(size) {
if (this.index < 5) {
this.push(`Chunk ${this.index++}\n`);
} else {
this.push(null); // No more data
}
}
}
class MyWritable extends Writable {
_write(chunk, encoding, callback) {
console.log(`Received: ${chunk.toString()}`);
callback();
}
}
const readableStream = new MyReadable();
const writableStream = new MyWritable();
readableStream.pipe(writableStream);
// Expected output:
// Received: Chunk 0
// Received: Chunk 1
// Received: Chunk 2
// Received: Chunk 3
// Received: Chunk 4