{"id":16079,"library":"http-status","title":"HTTP Status Codes Utility","description":"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.","status":"active","version":"2.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/adaltas/node-http-status","tags":["javascript","http","connect","frontend","status","express","typescript"],"install":[{"cmd":"npm install http-status","lang":"bash","label":"npm"},{"cmd":"yarn add http-status","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-status","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"For ESM, `status` is the default export. While `import { status }` also works due to dual export, the default is typically preferred.","wrong":"import { status } from 'http-status'","symbol":"status","correct":"import status from 'http-status'"},{"note":"Since v2, for CommonJS, the default export is accessed via `default` property. `require('http-status')` directly returns an object with the default export under the `default` key, among others. `const { status } = require('http-status')` is also common and recommended for named access.","wrong":"const status = require('http-status')","symbol":"status","correct":"const { default: status } = require('http-status')"},{"note":"Individual status code constants (like `NOT_FOUND` or `OK_CODE`) are directly exported as named exports for convenience.","wrong":"import status.NOT_FOUND from 'http-status'","symbol":"status","correct":"import { NOT_FOUND, OK_CODE } from 'http-status'"},{"note":"Specific categories of 'extra' status codes can be imported directly as default exports from their respective paths. This merges them with regular statuses.","wrong":"import { cloudflare } from 'http-status'","symbol":"status","correct":"import status from 'http-status/extra/cloudflare'"}],"quickstart":{"code":"import status from 'http-status';\n\nconsole.log('--- Standard HTTP Status Codes ---');\n// Look up status message by code\nconsole.log('200 Name:', status[200_NAME]); // Outputs: OK\nconsole.log('404 Message:', status[404_MESSAGE]); // Outputs: Not Found\nconsole.log('500 Class:', status[500_CLASS]); // Outputs: SERVER_ERROR\n\n// Look up code or message by status name\nconsole.log('OK Code:', status.OK_CODE); // Outputs: 200\nconsole.log('IM_A_TEAPOT Message:', status.IM_A_TEAPOT); // Outputs: I'm a teapot\n\nconsole.log('\\n--- Status Code Classes ---');\n// Access status code classes\nconsole.log('1xx Class Name:', status.classes[1].name); // Outputs: Informational\nconsole.log('2xx Message:', status.classes.SUCCESSFUL_MESSAGE); // Outputs: Successful\n\nconsole.log('\\n--- Extra Status Codes (Cloudflare) ---');\n// Access extra status codes via `extra` property\nconsole.log('Cloudflare 520:', status.extra.cloudflare[520_MESSAGE]); // Outputs: Unknown Error\nconsole.log('Cloudflare NO_RESPONSE Code:', status.extra.cloudflare.NO_RESPONSE_CODE); // Outputs: 444\n\n// Example of directly importing extra codes (requires separate import statement or direct require)\n// import cloudflareStatus from 'http-status/extra/cloudflare';\n// console.log('Cloudflare 522 Message:', cloudflareStatus[522_MESSAGE]);","lang":"typescript","description":"Demonstrates how to retrieve HTTP status code names, messages, and classes by both numeric code and symbolic name, including access to 'extra' status codes and class metadata."},"warnings":[{"fix":"For CommonJS, use `const { default: status } = require('http-status');` or `const status = require('http-status').default;`. For named exports, `const { NOT_FOUND } = require('http-status');` is correct.","message":"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.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Understand the API structure: `status[CODE]` gives the name, `status.NAME` gives the message, `status.NAME_CODE` gives the number. `status[CODE_NAME]` and `status[CODE_MESSAGE]` provide explicit access to the name and message respectively, similar to `status.NAME` and `status.NAME_CODE`.","message":"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').","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"To access specific extra codes, use `status.extra.cloudflare[520]` or `status.extra.nginx.NO_RESPONSE_CODE`. Alternatively, `import extraStatus from 'http-status/extra/cloudflare'` allows direct access to those codes, which are merged with common HTTP statuses in the imported object.","message":"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.","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":"Update CommonJS import: `const { default: status } = require('http-status');` or `const status = require('http-status').default;`.","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.","error":"TypeError: require(...) is not a function or is undefined"},{"fix":"If 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.","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.","error":"SyntaxError: Named export 'NOT_FOUND' not found. The requested module 'http-status' does not provide an export named 'NOT_FOUND'"}],"ecosystem":"npm"}