Vite-Compatible Readable Stream

raw JSON →
3.6.1 verified Mon Apr 27 auth: no javascript

A fork of readable-stream (v3.6.1) modified to remove circular dependencies, ensuring compatibility with Rollup and Vite production builds. It provides Node.js stream primitives (Readable, Writable, Transform, Duplex, PassThrough) in browser environments. Unlike the original readable-stream, this version eliminates circular require/import chains that cause Vite to fail during tree-shaking or bundling. It is primarily intended as a shim for libraries like react-pdf that depend on Node.js streams in browser contexts.

error Module parse failed: Unexpected token (1:44) at ... You may need an appropriate loader to handle this file type.
cause Using the original 'readable-stream' package in a Vite build without proper transpilation.
fix
Replace 'readable-stream' with 'vite-compatible-readable-stream' in package.json and imports.
error Uncaught TypeError: stream.push is not a function
cause Using the wrong stream class (e.g., Writable when you need Writeable).
fix
Ensure you are using correct class: Readable for readable streams, Writable for writable streams.
error Cannot find module 'vite-compatible-readable-stream'
cause Package not installed or not found in node_modules.
fix
Run npm install vite-compatible-readable-stream or add to package.json dependencies.
error The requested module 'vite-compatible-readable-stream' does not provide an export named 'Stream'
cause Importing a default 'Stream' export which does not exist.
fix
Import specific classes: import { Readable, Writable, Transform, Duplex, PassThrough } from 'vite-compatible-readable-stream'.
breaking vite-compatible-readable-stream v3.6.1 may not be compatible with Node.js versions below 6.
fix Upgrade to v3.6.1 or ensure Node.js >= 6.
gotcha This package removes circular dependencies from readable-stream, but it may not behave identically to the original readable-stream in edge cases.
fix Test thoroughly if migrating from readable-stream to this fork.
breaking Default export is the Readable class, not a stream factory. Use named exports for other stream types.
fix Change `import Stream from 'vite-compatible-readable-stream'` to `import { Readable } from 'vite-compatible-readable-stream'`.
gotcha Do not use the original 'readable-stream' package in Vite builds to avoid 'Module parse failed: Unexpected token' errors.
fix Replace all imports from 'readable-stream' with 'vite-compatible-readable-stream'.
deprecated The package's repository is a fork of readable-stream; future updates may lag behind upstream.
fix Monitor upstream readable-stream releases and consider alternatives if features are missing.
gotcha This package is intended for browser/Vite usage; in Node.js environments, prefer the native 'stream' module.
fix Use conditional imports or detect environment to switch between native stream and this polyfill.
npm install vite-compatible-readable-stream
yarn add vite-compatible-readable-stream
pnpm add vite-compatible-readable-stream

Creates and consumes a simple readable stream emitting numbers 1 to 5.

import { Readable } from 'vite-compatible-readable-stream';

// Create a readable stream that emits numbers 1 to 5
const numbersStream = new Readable({
  objectMode: true,
  read() {
    if (this.current === undefined) this.current = 1;
    if (this.current <= 5) {
      this.push(this.current++);
    } else {
      this.push(null);
    }
  }
});

// Consume the stream
numbersStream.on('data', (chunk) => console.log(chunk));
numbersStream.on('end', () => console.log('Stream ended.'));

// Output:
// 1
// 2
// 3
// 4
// 5
// Stream ended.