Node.js HTTP API for Browsers
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.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'request')
cause The `http` object was not correctly imported or `require`d, or it's being used in an environment where it's not available.fixEnsure 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). -
XMLHttpRequest cannot load [URL]. No 'Access-Control-Allow-Origin' header is present on the requested resource.
cause A cross-origin request was blocked by the browser's Same-Origin Policy. The server does not send the necessary CORS headers.fixEnsure 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. -
Response content-type header is 'text/plain; charset=x-user-defined' when expecting JSON/XML/etc.
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.fixRemove 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`.
Warnings
- breaking 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.
- gotcha 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.
- gotcha 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.
Install
-
npm install stream-http -
yarn add stream-http -
pnpm add stream-http
Imports
- http
const http = require('stream-http')import http from 'stream-http'
- http
import http from 'stream-http'
const http = require('stream-http') - METHODS, STATUS_CODES
import { METHODS, STATUS_CODES } from 'stream-http'import http from 'stream-http'; const { METHODS, STATUS_CODES } = http;
Quickstart
import http from 'stream-http';
// Example: Fetch data from a public API
const options = {
host: 'jsonplaceholder.typicode.com',
path: '/posts/1',
method: 'GET'
};
const req = http.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log('No more data in response.');
try {
const parsedData = JSON.parse(data);
console.log('Response body:', parsedData);
} catch (e) {
console.error('Failed to parse JSON:', e);
console.log('Raw response body:', data);
}
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
req.end();