HTTP Redirect Status Code Checker
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.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/is-redirect/index.js from ... not supported.
cause Attempting to import `is-redirect` (v2.0.0+) using `require()` in a CommonJS module.fixChange 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
cause 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).fixEither 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.
Warnings
- breaking The 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.
- breaking Version 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.
- gotcha The 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.
Install
-
npm install is-redirect -
yarn add is-redirect -
pnpm add is-redirect
Imports
- isRedirect
const isRedirect = require('is-redirect');import isRedirect from 'is-redirect';
- isRedirect (dynamic import)
const isRedirect = await import('is-redirect');const { default: isRedirect } = await import('is-redirect'); - type IsRedirect
import type { IsRedirect } from 'is-redirect';
Quickstart
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