{"id":16058,"library":"http-browserify","title":"Node.js HTTP Module for Browsers (Browserify)","description":"http-browserify provides a browser-compatible implementation of Node.js's native `http` module, specifically designed to be used with Browserify. When bundling browser-side code with Browserify, this package allows `require('http')` calls to function correctly, enabling HTTP requests from the browser environment using a Node.js-like API. The current stable version is 1.7.0. This package is part of the broader Browserify ecosystem, which focuses on bringing Node.js modules to the browser. It implements core HTTP client functionalities such as `http.request`, `http.get`, and methods for managing headers and data streams. Key differentiators include its tight integration with Browserify's module resolution and its effort to mimic the Node.js API, making it easy for developers to port server-side HTTP logic to the client.","status":"maintenance","version":"1.7.0","language":"javascript","source_language":"en","source_url":"http://github.com/substack/http-browserify","tags":["javascript","http","browserify","compatible","meatless","browser"],"install":[{"cmd":"npm install http-browserify","lang":"bash","label":"npm"},{"cmd":"yarn add http-browserify","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-browserify","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is designed for CommonJS environments and Browserify bundling. It directly shims Node.js's 'http' module.","symbol":"http","correct":"const http = require('http');"},{"note":"When using Browserify, `http-browserify` typically transparently replaces the Node.js 'http' module. If explicit mapping is needed, use the `require` option during bundling as shown.","wrong":"var bundle = browserify().require('http-browserify', { expose: 'http' });","symbol":"http","correct":"var bundle = browserify({ require : { http : 'http-browserify' } });"}],"quickstart":{"code":"const http = require('http');\n\nconst resultDiv = document.createElement('div');\nresultDiv.id = 'result';\ndocument.body.appendChild(resultDiv);\n\n// Mock server response for demonstration in a real browserify context\n// In a true browser environment, this would hit an actual endpoint.\nconst mockEndpoint = '/beep';\n\nconsole.log(`Making a GET request to ${mockEndpoint}`);\n\nhttp.get({ path : mockEndpoint }, function (res) {\n    resultDiv.innerHTML += `GET ${mockEndpoint}<br>`;\n    \n    res.on('data', function (buf) {\n        resultDiv.innerHTML += `Received data: ${buf}<br>`;\n    });\n    \n    res.on('end', function () {\n        resultDiv.innerHTML += '<br>__END__';\n        console.log('Request ended.');\n    });\n\n    res.on('error', function(err) {\n        resultDiv.innerHTML += `Error: ${err.message}<br>`;\n        console.error('Request error:', err);\n    });\n});\n\n// To make this runnable without a backend, you'd typically intercept the request\n// For a real-world scenario, '/beep' would be served by a backend.\n// This example demonstrates the client-side API usage.\n","lang":"javascript","description":"Demonstrates how to perform a basic GET request using the `http` module API in a Browserify-bundled environment, handling data and end events."},"warnings":[{"fix":"Be aware of buffering behavior differences when targeting older browsers. Implement client-side buffering or progressive rendering if necessary for older browser compatibility, or ensure modern browser targets for true streaming.","message":"Multipart streaming responses are buffered in older browsers (Internet Explorer 5.5-9, Opera 10.6). Modern browsers (Firefox 3.5+, Chrome 7.0+, Safari 5.0+) provide unbuffered streams for content-types like `multipart/octet-stream`.","severity":"gotcha","affected_versions":"<=1.7.0"},{"fix":"For new projects, consider using `fetch` or `XMLHttpRequest` directly for HTTP requests, or leverage modern bundler features for polyfilling if Node.js-like APIs are preferred. For existing Browserify projects, continue using `http-browserify`.","message":"`http-browserify` is primarily designed for the Browserify ecosystem. While still functional, newer bundling tools like Webpack and Rollup often provide their own strategies for polyfilling Node.js APIs or encourage explicit browser-native APIs (e.g., `fetch` or `XMLHttpRequest`).","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Always explicitly set `opts.host` and `opts.port` (and `opts.protocol` if necessary) when making requests to a different origin to avoid unexpected same-origin requests. Be mindful of CORS implications for cross-origin requests.","message":"The `http.request` `opts.host` and `opts.port` default to `window.location.host` and `window.location.port` respectively. This means requests are implicitly made to the same origin unless explicitly specified.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Ensure your JavaScript code is bundled using Browserify. Run `browserify your-entry-file.js -o bundle.js` and include `bundle.js` in your HTML. `http-browserify` only works when processed by Browserify.","cause":"`require('http')` is being called in a browser environment without prior bundling by Browserify.","error":"Uncaught ReferenceError: require is not defined"},{"fix":"Ensure the callback passed to `http.get` or `http.request` properly receives the `res` object. Check for network errors or server issues that might prevent a valid HTTP response, or handle errors on the `req` object itself.","cause":"The `res` (response) object from `http.get` or `http.request` might not be fully initialized or an error occurred before the response stream was ready.","error":"TypeError: Cannot read properties of undefined (reading 'on') when calling res.on('data')"}],"ecosystem":"npm"}