HTTP Status Codes Utility
http-status is a utility library for Node.js that provides comprehensive interaction with HTTP status codes, their names, messages, and classes. Currently at version 2.1.0, it offers a stable API for looking up information by either a numeric code or a symbolic name (e.g., `status[404]` or `status.NOT_FOUND`). A key differentiator is its inclusion of both standard IANA codes and an extensive set of 'extra' codes used by popular software like IIS, NGINX, and Cloudflare, which are categorized and accessible. The library is written in TypeScript and supports both ESM and CommonJS module systems, with a focus on ease of use across modern JavaScript environments. It doesn't specify a strict release cadence but shows active maintenance through its recent major version migration to modern module standards.
Common errors
-
TypeError: require(...) is not a function or is undefined
cause Attempting to use `require('http-status')` as a direct function call or expecting it to return the default export directly after the v2 migration for CommonJS.fixUpdate CommonJS import: `const { default: status } = require('http-status');` or `const status = require('http-status').default;`. -
SyntaxError: Named export 'NOT_FOUND' not found. The requested module 'http-status' does not provide an export named 'NOT_FOUND'
cause Incorrectly trying to destructure a named export in an ESM context when the module primarily offers a default export for the main `status` object.fixIf you want the main `status` object, use `import status from 'http-status';`. If you truly want a named export, ensure it's specifically exported (e.g., `OK_CODE` or `NOT_FOUND` are indeed named exports, so `import { NOT_FOUND } from 'http-status'` is correct). This error often implies confusion between default and named exports.
Warnings
- breaking Version 2.x migrated the library to ESM modules and TypeScript. While the API remains largely the same, CommonJS `require` statements need to be updated to access the default export correctly.
- gotcha The package includes `_NAME`, `_MESSAGE`, and `_CLASS` suffixes for accessing specific attributes of a status code (e.g., `status[404_NAME]`, `status.NOT_FOUND_MESSAGE`). For the primary status code message, the code name itself is sufficient (e.g., `status.NOT_FOUND` returns 'Not Found').
- gotcha When accessing 'extra' status codes, they are nested under `status.extra.<category>` (e.g., `status.extra.cloudflare`). Direct top-level access to these non-standard codes is not available unless explicitly imported from a sub-path.
Install
-
npm install http-status -
yarn add http-status -
pnpm add http-status
Imports
- status
import { status } from 'http-status'import status from 'http-status'
- status
const status = require('http-status')const { default: status } = require('http-status') - status
import status.NOT_FOUND from 'http-status'
import { NOT_FOUND, OK_CODE } from 'http-status' - status
import { cloudflare } from 'http-status'import status from 'http-status/extra/cloudflare'
Quickstart
import status from 'http-status';
console.log('--- Standard HTTP Status Codes ---');
// Look up status message by code
console.log('200 Name:', status[200_NAME]); // Outputs: OK
console.log('404 Message:', status[404_MESSAGE]); // Outputs: Not Found
console.log('500 Class:', status[500_CLASS]); // Outputs: SERVER_ERROR
// Look up code or message by status name
console.log('OK Code:', status.OK_CODE); // Outputs: 200
console.log('IM_A_TEAPOT Message:', status.IM_A_TEAPOT); // Outputs: I'm a teapot
console.log('\n--- Status Code Classes ---');
// Access status code classes
console.log('1xx Class Name:', status.classes[1].name); // Outputs: Informational
console.log('2xx Message:', status.classes.SUCCESSFUL_MESSAGE); // Outputs: Successful
console.log('\n--- Extra Status Codes (Cloudflare) ---');
// Access extra status codes via `extra` property
console.log('Cloudflare 520:', status.extra.cloudflare[520_MESSAGE]); // Outputs: Unknown Error
console.log('Cloudflare NO_RESPONSE Code:', status.extra.cloudflare.NO_RESPONSE_CODE); // Outputs: 444
// Example of directly importing extra codes (requires separate import statement or direct require)
// import cloudflareStatus from 'http-status/extra/cloudflare';
// console.log('Cloudflare 522 Message:', cloudflareStatus[522_MESSAGE]);