{"id":16513,"library":"readable-web-to-node-stream","title":"Web ReadableStream to Node.js Readable Stream Converter","description":"`readable-web-to-node-stream` is a utility package designed to seamlessly convert a Web-API `ReadableStream` (typically obtained from browser `fetch` responses or other Web Streams API sources) into a Node.js `Readable` stream. This conversion allows developers to process data originating from web contexts using familiar Node.js stream APIs and utilities, integrating browser-side streaming capabilities with Node.js backend processing. The current stable version is 5.0.0. The package maintains an active release cadence, frequently incorporating improvements and addressing breaking changes, notably the migration to a pure ECMAScript Module (ESM) in version 4.0.0. Key differentiators include its singular focus on this specific conversion direction, first-class TypeScript support, and a clear distinction from its complementary package for the reverse conversion. It targets modern JavaScript environments, requiring Node.js version 18 or newer.","status":"active","version":"5.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/Borewit/readable-web-to-node-stream","tags":["javascript","stream.readable","web","node","browser","stream","covert","coverter","readable","typescript"],"install":[{"cmd":"npm install readable-web-to-node-stream","lang":"bash","label":"npm"},{"cmd":"yarn add readable-web-to-node-stream","lang":"bash","label":"yarn"},{"cmd":"pnpm add readable-web-to-node-stream","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since v4.0.0, the package is pure ESM. Use 'import' syntax. CommonJS 'require()' is only compatible in Node.js >= 22 or with specific loader configurations. Prior to v4, `require` was the standard.","wrong":"const ReadableWebToNodeStream = require('readable-web-to-node-stream');","symbol":"ReadableWebToNodeStream","correct":"import { ReadableWebToNodeStream } from 'readable-web-to-node-stream';"},{"note":"For TypeScript, use 'import type' for the class constructor if you only need the type definition without bundling the runtime implementation. This is often an optimization, though `ReadableWebToNodeStream` is typically instantiated at runtime.","symbol":"ReadableWebToNodeStream (Type)","correct":"import type { ReadableWebToNodeStream } from 'readable-web-to-node-stream';"},{"note":"Imports all named exports (in this case, primarily `ReadableWebToNodeStream`) into a namespace object. The package does not provide a default export, so attempting to use a default import will fail.","wrong":"import WebToNodeStream from 'readable-web-to-node-stream';","symbol":"All exports (namespace import)","correct":"import * as WebToNodeStream from 'readable-web-to-node-stream';"}],"quickstart":{"code":"import { ReadableWebToNodeStream } from 'readable-web-to-node-stream';\nimport { createWriteStream } from 'fs';\nimport { pipeline } from 'stream/promises';\n\nasync function downloadAndSave(url: string, filePath: string) {\n    console.log(`Downloading ${url} and saving to ${filePath}`);\n    // Ensure 'fetch' is available (global in browsers, or polyfilled/imported in Node.js < 18 or for specific use cases)\n    const response = await fetch(url);\n    if (!response.body) {\n        throw new Error('Response body is null. Cannot convert an empty body.');\n    }\n    const webReadableStream = response.body;\n    \n    // Convert the Web-API ReadableStream to a Node.js Readable stream\n    const nodeStream = new ReadableWebToNodeStream(webReadableStream, { propagateDestroy: true });\n\n    // Pipe the Node.js stream to a file using stream.pipeline for robust error handling and cleanup\n    await pipeline(\n        nodeStream,\n        createWriteStream(filePath)\n    );\n    console.log(`Download complete: ${filePath}`);\n}\n\n// Example usage: Downloads a small dummy file from a public URL\n// Replace with a suitable URL for actual testing. For local testing, a simple text file is good.\nconst exampleUrl = 'https://raw.githubusercontent.com/Borewit/readable-web-to-node-stream/master/package.json';\nconst outputFilePath = './downloaded-package.json';\n\ndownloadAndSave(exampleUrl, outputFilePath)\n    .catch(error => console.error('An error occurred during download:', error));\n","lang":"typescript","description":"Demonstrates converting a `fetch` response's Web-API ReadableStream into a Node.js Readable stream and piping it to a file, including error handling."},"warnings":[{"fix":"Update all CommonJS `require()` statements to ESM `import` statements. For older Node.js versions, consider using dynamic `import()` or migrating your project to ESM. Node.js >= 22 provides experimental CJS compatibility for ESM packages.","message":"The package migrated to a pure ECMAScript Module (ESM) in version 4.0.0. CommonJS `require()` is no longer directly supported in Node.js versions below 22, requiring changes to import statements.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Explicitly set `{ propagateDestroy: true }` in the `ReadableWebToNodeStream` constructor options if you want the Node.js stream's `destroy` method to also cancel the source Web-API stream, matching behavior of some previous stream implementations or expectations.","message":"The constructor option `propagateDestroy` was introduced in v5.0.0. Its default value is `false`. If set to `true`, destroying the Node.js stream will also cancel the underlying Web-API `ReadableStream`.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Upgrade your Node.js environment to version 18 or higher to use versions 4.0.0 and above of this package.","message":"The Node.js engine requirement was updated to `>= 18` starting from version 4.0.0. This package will not run on older Node.js versions.","severity":"gotcha","affected_versions":">=4.0.0"},{"fix":"Upgrade to version 3.0.2 or higher to patch known security vulnerabilities in your project's dependency tree.","message":"Multiple security vulnerabilities were addressed in dependencies (e.g., elliptic, y18n, ssri, lodash) in version 3.0.2. Older versions `<=3.0.1` may contain known vulnerabilities.","severity":"gotcha","affected_versions":"<=3.0.1"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Replace `const { ReadableWebToNodeStream } = require('readable-web-to-node-stream');` with `import { ReadableWebToNodeStream } from 'readable-web-to-node-stream';`. Ensure your project is configured for ESM (e.g., `\"type\": \"module\"` in `package.json`) or upgrade to Node.js v22+ if you need CJS compatibility with ESM packages.","cause":"Attempting to import the `readable-web-to-node-stream` package (which is pure ESM since v4.0.0) using CommonJS `require()` syntax in a Node.js environment older than v22 or without explicit ESM configuration.","error":"ERR_REQUIRE_ESM: require() of ES Module ... is not supported. Instead change the require of index.js in ... to a dynamic import() call."},{"fix":"Ensure you are running in a browser environment or in a Node.js environment where a global `fetch` API (which commonly provides `ReadableStream` on its response bodies) is available or explicitly polyfilled. For Node.js < 18, or if `fetch` is not globally available, you might need to use a package like `node-fetch` and/or `web-streams-polyfill`.","cause":"The Web-API `ReadableStream` class is not available in the global scope where the code is executed. This typically happens in a pure Node.js environment without a browser-like global `fetch` API or a polyfill for web streams.","error":"ReferenceError: ReadableStream is not defined"}],"ecosystem":"npm"}