Node.js Stream API for Browsers (Browserify)

3.0.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Demonstrates creating and piping a basic readable stream to a writable stream using `stream-browserify`.

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

view raw JSON →