{"id":16908,"library":"stream-http","title":"Node.js HTTP API for Browsers","description":"stream-http is a JavaScript library designed to provide a browser-compatible implementation of Node.js's native `http` module. Its primary goal is to replicate the Node.js HTTP client API and behavior as closely as possible within the constraints of web browsers, making it suitable for projects that need consistent HTTP request handling across Node.js and browser environments. The package is currently at version 3.2.0 and appears to be actively maintained, indicated by recent feature additions and ongoing support for modern browser capabilities. A key differentiator is its emphasis on streaming, delivering data to the caller before the request fully completes. It supports true streaming with backpressure in Chrome >= 58 via `fetch` and `WritableStream`, true streaming in Chrome >= 43 (via `fetch`) and Firefox >= 9 (via `moz-chunked-arraybuffer`), and pseudo-streaming in other supported browsers where the full response is held in memory but available early. It aims to replace `http-browserify`. It also provides additional browser-specific features like `message.url` for redirects and `options.withCredentials` for CORS requests.","status":"active","version":"3.2.0","language":"javascript","source_language":"en","source_url":"git://github.com/jhiesey/stream-http","tags":["javascript","http","stream","streaming","xhr","http-browserify"],"install":[{"cmd":"npm install stream-http","lang":"bash","label":"npm"},{"cmd":"yarn add stream-http","lang":"bash","label":"yarn"},{"cmd":"pnpm add stream-http","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For modern module bundlers and ESM environments. This is the recommended way.","wrong":"const http = require('stream-http')","symbol":"http","correct":"import http from 'stream-http'"},{"note":"CommonJS style, typically used with older bundlers like Browserify or Webpack < 5 without specific ESM configuration.","wrong":"import http from 'stream-http'","symbol":"http","correct":"const http = require('stream-http')"},{"note":"METHODS and STATUS_CODES are properties of the default `http` export, not named exports from the module root.","wrong":"import { METHODS, STATUS_CODES } from 'stream-http'","symbol":"METHODS, STATUS_CODES","correct":"import http from 'stream-http'; const { METHODS, STATUS_CODES } = http;"}],"quickstart":{"code":"import http from 'stream-http';\n\n// Example: Fetch data from a public API\nconst options = {\n  host: 'jsonplaceholder.typicode.com',\n  path: '/posts/1',\n  method: 'GET'\n};\n\nconst req = http.request(options, (res) => {\n  console.log(`STATUS: ${res.statusCode}`);\n  console.log(`HEADERS: ${JSON.stringify(res.headers)}`);\n\n  let data = '';\n  res.on('data', (chunk) => {\n    data += chunk;\n  });\n\n  res.on('end', () => {\n    console.log('No more data in response.');\n    try {\n      const parsedData = JSON.parse(data);\n      console.log('Response body:', parsedData);\n    } catch (e) {\n      console.error('Failed to parse JSON:', e);\n      console.log('Raw response body:', data);\n    }\n  });\n});\n\nreq.on('error', (e) => {\n  console.error(`problem with request: ${e.message}`);\n});\n\nreq.end();","lang":"javascript","description":"This quickstart demonstrates how to make a GET request using the `http.request` method, similar to Node.js. It shows how to set request options, handle the response stream, and process incoming data chunks until the request ends, including error handling."},"warnings":[{"fix":"Upgrade to a modern browser (IE11+) or use an older version of `stream-http` if IE10- support is critical.","message":"As of version 3.0.0, Internet Explorer 10 and below are no longer supported. IE11 support remains but users of older IE versions must stick to stream-http v2 or earlier.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Use the default `options.mode` for general purpose requests, especially with binary data or when accurate Content-Type headers are critical. Only use 'prefer-streaming' for known text data.","message":"Using `options.mode` set to 'allow-wrong-content-type' or 'prefer-streaming' can lead to incorrect 'content-type' response headers (e.g., 'text/plain; charset=x-user-defined') in some browsers, notably Safari and Chrome 42 and older. 'prefer-streaming' can also corrupt binary data.","severity":"gotcha","affected_versions":">=1.5.0"},{"fix":"Review the `stream-http` documentation and browser API capabilities for specific features that might not be fully supported. Adjust application logic to account for browser-specific behaviors like silent redirects.","message":"Due to inherent browser limitations, `stream-http` cannot replicate the Node.js `http` module's API and behavior with 100% fidelity. Specific features like fine-grained control over redirects or certain socket options are unavailable.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure you are using the correct `import` or `require` statement for your module system (e.g., `import http from 'stream-http'` for ESM, `const http = require('stream-http')` for CommonJS).","cause":"The `http` object was not correctly imported or `require`d, or it's being used in an environment where it's not available.","error":"TypeError: Cannot read properties of undefined (reading 'request')"},{"fix":"Ensure the server you are requesting from is configured to send `Access-Control-Allow-Origin` headers that permit requests from your domain. For requests that need to send cookies or authentication, set `options.withCredentials = true` in your request configuration.","cause":"A cross-origin request was blocked by the browser's Same-Origin Policy. The server does not send the necessary CORS headers.","error":"XMLHttpRequest cannot load [URL]. No 'Access-Control-Allow-Origin' header is present on the requested resource."},{"fix":"Remove or set `options.mode` to 'default' (or `undefined`) unless you explicitly understand and accept the content-type reporting inaccuracies for streaming benefits. Verify browser compatibility for your chosen `mode`.","cause":"The `options.mode` was set to 'allow-wrong-content-type' or 'prefer-streaming', which can override or misreport the actual Content-Type header in some older browsers or specific configurations.","error":"Response content-type header is 'text/plain; charset=x-user-defined' when expecting JSON/XML/etc."}],"ecosystem":"npm","meta_description":null}