{"id":16035,"library":"follow-redirects","title":"Follow Redirects","description":"follow-redirects is a Node.js utility that provides a drop-in replacement for the native `http` and `https` modules, automatically handling HTTP and HTTPS redirects transparently. Currently stable at version 1.16.0, the package sees regular maintenance with patch and minor releases, indicating active development and bug fixes. Its core functionality enables developers to make network requests without manually inspecting and reissuing requests for 3xx status codes, simplifying client-side HTTP interactions significantly. Unlike the built-in Node.js modules, `follow-redirects` abstracts away the complexity of redirect chains, offering a more robust and developer-friendly experience for fetching resources across multiple redirects. It provides configurable limits for maximum redirects and response body length, which can be set both globally for all requests or individually for specific requests.","status":"active","version":"1.16.0","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/follow-redirects/follow-redirects","tags":["javascript","http","https","url","redirect","client","location","utility"],"install":[{"cmd":"npm install follow-redirects","lang":"bash","label":"npm"},{"cmd":"yarn add follow-redirects","lang":"bash","label":"yarn"},{"cmd":"pnpm add follow-redirects","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Primary usage is CommonJS, reflecting the module's origins and common Node.js integration patterns. While ESM import may work with transpilers, `require` is the canonical and most reliable method.","wrong":"import { http, https } from 'follow-redirects';","symbol":"{ http, https }","correct":"const { http, https } = require('follow-redirects');"},{"note":"Use this pattern to access and configure global options like `followRedirects.maxRedirects` or `followRedirects.maxBodyLength`.","wrong":"import followRedirects from 'follow-redirects';","symbol":"followRedirects (module object)","correct":"const followRedirects = require('follow-redirects');"},{"note":"The package ships with TypeScript declarations, allowing for type-safe usage of its options and interfaces.","symbol":"Types (for TypeScript)","correct":"import type { HttpOptions, HttpsOptions, RedirectOptions, ClientRequest, IncomingMessage } from 'follow-redirects';"}],"quickstart":{"code":"const { http, https } = require('follow-redirects');\nconst url = require('url');\n\n// Example 1: Simple GET request with redirect following\n// Using a reliable test service for redirects\nhttp.get('http://httpbin.org/redirect/3', response => {\n  console.log(`GET Status Code: ${response.statusCode}`);\n  console.log(`GET Final URL: ${response.responseUrl}`);\n  let data = '';\n  response.on('data', chunk => {\n    data += chunk;\n  });\n  response.on('end', () => {\n    console.log('GET Response body length:', data.length);\n  });\n}).on('error', err => {\n  console.error('GET Error:', err.message);\n});\n\n// Example 2: POST request demonstrating global and per-request options\n// Setting global max redirects (default is 21)\nfollowRedirects.maxRedirects = 5; \n// Setting global max body length (default is 10MB)\nfollowRedirects.maxBodyLength = 1 * 1024 * 1024; // 1 MB\n\nconst postOptions = url.parse('https://postman-echo.com/post');\npostOptions.method = 'POST';\npostOptions.headers = {\n  'Content-Type': 'application/json',\n  'User-Agent': 'follow-redirects-example/1.0'\n};\n// Override global maxRedirects for this specific POST request\npostOptions.maxRedirects = 2;\n\nconst postData = JSON.stringify({ message: 'Hello from follow-redirects!', source: 'quickstart' });\n\nconst postReq = https.request(postOptions, response => {\n  console.log(`POST Status Code: ${response.statusCode}`);\n  console.log(`POST Final URL: ${response.responseUrl}`);\n  let data = '';\n  response.on('data', chunk => {\n    data += chunk;\n  });\n  response.on('end', () => {\n    console.log('POST Response body:', JSON.parse(data));\n  });\n}).on('error', err => {\n  console.error('POST Error:', err.message);\n});\n\npostReq.write(postData);\npostReq.end();","lang":"javascript","description":"This quickstart demonstrates basic GET and POST requests using `follow-redirects`, showing how to retrieve the final URL after redirects and how to configure both global and per-request options for maximum redirects and body length."},"warnings":[{"fix":"Increase `maxRedirects` in your options if legitimate redirects exceed the default, or investigate the target URL for misconfigurations causing excessive redirects.","message":"Exceeding the `maxRedirects` limit (default: 21) will cause an error to be emitted instead of returning a response. Ensure your requests do not enter infinite redirect loops or exceed this count for valid reasons, and handle the error.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Increase `maxBodyLength` in your options if you legitimately need to send larger request bodies, or ensure your request body size is within limits.","message":"Sending a request body larger than `maxBodyLength` (default: 10MB) will result in an error. This prevents potential denial-of-service attacks or accidental oversized uploads.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always use per-request options (`options.maxRedirects`, `options.maxBodyLength`) when specific limits are required, or when running in an environment where other parts of the application might use `follow-redirects` with different expectations.","message":"Global options (`followRedirects.maxRedirects`, `followRedirects.maxBodyLength`) affect all subsequent requests made via `follow-redirects` unless explicitly overridden by per-request options. This can lead to unexpected behavior if not managed carefully in a shared environment.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"If you need both the original and final URLs, capture the initial request URL before making the call.","message":"The `responseUrl` property on the `response` object provides the final URL after all redirects have been followed. The original request URL is not directly available on the final response object; if needed, it must be stored prior to the request.","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":"Inspect the URL chain for infinite redirects or increase the `maxRedirects` option if a higher number of legitimate redirects is expected (e.g., `options.maxRedirects = 30;`).","cause":"The HTTP/HTTPS request encountered more redirects than the configured `maxRedirects` limit.","error":"Error: Too many redirects. Max of 21 reached."},{"fix":"Reduce the size of the request body, or increase the `maxBodyLength` option if larger payloads are necessary (e.g., `options.maxBodyLength = 50 * 1024 * 1024;` for 50MB).","cause":"The size of the data being sent in the request body exceeded the configured `maxBodyLength` limit.","error":"Error: Request body exceeded maxBodyLength limit."},{"fix":"Ensure your project is configured for CommonJS, or use dynamic `import()` if you absolutely need to load `follow-redirects` in an ESM context, though its primary API is CommonJS-oriented.","cause":"Attempting to use `require()` syntax in an ES Module (ESM) context.","error":"ReferenceError: require is not defined"}],"ecosystem":"npm"}