Registry / http-networking / is-redirect

is-redirect

JSON →
library2.0.0jsnpmunverified

is-redirect is a lightweight, focused utility package for JavaScript and TypeScript environments that efficiently determines if a given numeric HTTP status code falls within the 3xx redirection range. The current stable version is 2.0.0. As part of Sindre Sorhus's collection of focused utilities, it follows a pragmatic release cadence, with major versions typically signaling significant breaking changes, such as the recent transition to pure ESM. Its primary differentiator is its singular focus and minimal footprint, providing a reliable, dependency-free check for redirect status codes without including additional HTTP client or server logic. It serves as a fundamental building block for applications that need to interpret HTTP responses, such as proxies, web scrapers, or API clients, ensuring correct handling of redirect flows.

npm install is-redirect
INSTALL
IMPORT
SIG · IS-REDIRECT
I
is-redirect
http-networkingjavascriptv2.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

isRedirect
import isRedirect from 'is-redirect';
const isRedirect = require('is-redirect');
Package is pure ESM since v2.0.0; CommonJS `require` is no longer supported and will result in an `ERR_REQUIRE_ESM` error. Ensure your project is configured for ESM.
isRedirect (dynamic import)
const { default: isRedirect } = await import('is-redirect');
const isRedirect = await import('is-redirect');
When dynamically importing, the default export is nested under a `default` property. Destructure correctly or access `importedModule.default`.
type IsRedirect
import type { IsRedirect } from 'is-redirect';
TypeScript users can import the type definition for the function if needed for advanced type-checking scenarios, though it's typically inferred.

Demonstrates how to use `is-redirect` to categorize different HTTP status codes, highlighting its primary function within a broader status checking logic.

import isRedirect from 'is-redirect'; function categorizeStatusCode(statusCode) { if (isRedirect(statusCode)) { console.log(`Status ${statusCode}: This is a redirect code.`); return 'redirect'; } else if (statusCode >= 200 && statusCode < 300) { console.log(`Status ${statusCode}: This is a success code.`); return 'success'; } else if (statusCode >= 400 && statusCode < 500) { console.log(`Status ${statusCode}: This is a client error code.`); return 'client-error'; } else if (statusCode >= 500 && statusCode < 600) { console.log(`Status ${statusCode}: This is a server error code.`); return 'server-error'; } else { console.log(`Status ${statusCode}: Uncategorized.`); return 'other'; } } console.log('\n--- Testing various HTTP status codes ---'); categorizeStatusCode(301); // Moved Permanently categorizeStatusCode(302); // Found categorizeStatusCode(307); // Temporary Redirect categorizeStatusCode(200); // OK categorizeStatusCode(404); // Not Found categorizeStatusCode(500); // Internal Server Error categorizeStatusCode(100); // Continue
Debug
Known issues
breakingThe package `is-redirect` is now pure ESM (ECMAScript Modules) starting from version 2.0.0. This means it can no longer be imported using CommonJS `require()` syntax.
fix
Migrate your project to use ESM imports (`import isRedirect from 'is-redirect';`) or ensure your consuming module is also ESM. For Node.js, this often involves setting `"type": "module"` in your `package.json` or using `.mjs` file extensions.
affects: >=2.0.0
breakingVersion 2.0.0 and above of `is-redirect` requires Node.js version 12 or higher. Older Node.js versions will not be able to run this package.
fix
Upgrade your Node.js environment to version 12 or newer. If you must support older Node.js versions, you will need to stick to `is-redirect@1.x`.
affects: >=2.0.0
gotchaThe package only checks for 3xx status codes commonly associated with redirects. It does not handle edge cases like `101 Switching Protocols` or `204 No Content` which might imply a redirect-like behavior in specific application contexts but are not strictly 3xx redirects.
fix
For comprehensive HTTP status code handling beyond 3xx redirects, integrate `is-redirect` with other status code checking utilities or custom logic for different HTTP ranges (e.g., `is-success`, `is-server-error`).
affects: >=1.0.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/is-redirect/index.js from ... not supported.
Attempting to import `is-redirect` (v2.0.0+) using `require()` in a CommonJS module.
fix
Change your import statement to `import isRedirect from 'is-redirect';` and ensure your file/project is configured for ESM (e.g., `"type": "module"` in `package.json`).
SyntaxError: Cannot use import statement outside a module
Using an `import` statement for `is-redirect` (v2.0.0+) in a JavaScript file that is being interpreted as a CommonJS module (e.g., a `.js` file without `"type": "module"` in `package.json` and not an `.mjs` file).
fix
Either rename your file to `.mjs` or add `"type": "module"` to your project's `package.json` to enable ESM for `.js` files. Alternatively, if your project needs to remain CommonJS, downgrade `is-redirect` to version 1.x.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
is-redirect — npm install is-redirect · libregistry