{"id":16390,"library":"http-status-codes","title":"HTTP Status Codes Constants","description":"http-status-codes is a JavaScript/TypeScript library that provides constants for HTTP status codes and their corresponding reason phrases. It is currently at version 2.3.0 and maintains a regular release cadence, primarily adding new RFC-defined status codes and minor improvements. Key differentiators include its complete lack of external dependencies, its full support for modern JavaScript and TypeScript environments, and its comprehensive coverage of status codes defined across various RFCs (like RFC1945, RFC2616, RFC2518, RFC6585, RFC7538, RFC8297, RFC7231, RFC7540). The library is designed to be highly tree-shakable, reducing bundle sizes for applications only using a subset of its features. It also offers utility functions to retrieve reason phrases from codes and vice versa, making it a robust and convenient tool for handling HTTP responses.","status":"active","version":"2.3.0","language":"javascript","source_language":"en","source_url":"https://github.com/prettymuchbryce/http-status-codes","tags":["javascript","node","http","apache","HttpStatus","httpclient","status","codes"],"install":[{"cmd":"npm install http-status-codes","lang":"bash","label":"npm"},{"cmd":"yarn add http-status-codes","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-status-codes","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since v2.0.0, the primary usage is named imports, particularly with TypeScript enums. CommonJS `require` is supported but named imports are generally preferred for tree-shaking and modern module systems.","wrong":"const StatusCodes = require('http-status-codes').StatusCodes;","symbol":"StatusCodes","correct":"import { StatusCodes } from 'http-status-codes';"},{"note":"Provides human-readable reason phrases. Prior to v2.0.0, these were part of a different API (e.g., via `getStatusText`).","wrong":"const ReasonPhrases = require('http-status-codes').ReasonPhrases;","symbol":"ReasonPhrases","correct":"import { ReasonPhrases } from 'http-status-codes';"},{"note":"The function was renamed from `getStatusText` to `getReasonPhrase` in v2.0.0 for API consistency. Using `getStatusText` will result in an error on v2.0.0 and later.","wrong":"import { getStatusText } from 'http-status-codes';","symbol":"getReasonPhrase","correct":"import { getReasonPhrase } from 'http-status-codes';"},{"note":"This function was added in v1.4.0. It retrieves a status code from its reason phrase string.","wrong":"import { getCode } from 'http-status-codes';","symbol":"getStatusCode","correct":"import { getStatusCode } from 'http-status-codes';"}],"quickstart":{"code":"import { StatusCodes, ReasonPhrases, getReasonPhrase, getStatusCode } from 'http-status-codes';\n\n// Example usage with a hypothetical 'response' object (e.g., from Express)\nconst response = {\n  _status: 200,\n  _data: '',\n  status(code) {\n    this._status = code;\n    return this;\n  },\n  send(data) {\n    this._data = data;\n    console.log(`Status: ${this._status}, Body: ${JSON.stringify(this._data)}`);\n  }\n};\n\n// Send a successful response\nresponse\n  .status(StatusCodes.OK)\n  .send(ReasonPhrases.OK);\n\n// Handle an internal server error\nresponse\n  .status(StatusCodes.INTERNAL_SERVER_ERROR)\n  .send({\n    error: getReasonPhrase(StatusCodes.INTERNAL_SERVER_ERROR)\n  });\n\n// Get status code from a reason phrase string\nconst codeFromPhrase = getStatusCode('Bad Request');\nresponse\n  .status(codeFromPhrase)\n  .send({\n    error: 'An unknown error occurred.'\n  });","lang":"typescript","description":"Demonstrates importing and using status code constants, reason phrases, and utility functions to manage HTTP responses."},"warnings":[{"fix":"Replace all instances of `getStatusText` with `getReasonPhrase`.","message":"The function `getStatusText` was renamed to `getReasonPhrase` in version 2.0.0. Code using `getStatusText` will break.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Update any logic that checks for the `500` reason phrase to use `ReasonPhrases.INTERNAL_SERVER_ERROR` or the string 'Internal Server Error'.","message":"The reason phrase for `500 Internal Server Error` was corrected from 'Server Error' to 'Internal Server Error' in v2.0.0. If you were relying on the exact string 'Server Error', your comparisons might fail.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Ensure your build setup correctly handles TypeScript declarations if you are consuming this library in a non-TypeScript project or an older TypeScript environment.","message":"The library was rewritten in TypeScript starting from v2.0.0, introducing TypeScript enums for `StatusCodes` and `ReasonPhrases`. While this improves type safety, it also means that projects not using TypeScript might notice internal structure changes, though the public API is largely consistent apart from explicit breaking changes.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Upgrade to `http-status-codes@2.1.4` or newer to benefit from improved tree-shaking and reduced bundle sizes.","message":"From v2.1.4, significant tree-shaking improvements were introduced. Older versions (pre-2.1.4) might result in larger bundle sizes, especially if only a subset of constants is used.","severity":"gotcha","affected_versions":"<2.1.4"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Rename `getStatusText` to `getReasonPhrase`. Example: `import { getReasonPhrase } from 'http-status-codes';`","cause":"Attempting to use the `getStatusText` function from version 1.x with `http-status-codes` version 2.x or newer.","error":"TypeError: (0, http_status_codes_1.getStatusText) is not a function"},{"fix":"For CommonJS, use `const { StatusCodes } = require('http-status-codes');` or `const StatusCodes = require('http-status-codes').StatusCodes;`. For ESM, ensure `import { StatusCodes } from 'http-status-codes';` is used.","cause":"This typically occurs when trying to `require` named exports (e.g., `StatusCodes.OK`) in a CommonJS module in a way that doesn't correctly resolve the export, or when a bundler has misconfigured ESM/CJS interop. Alternatively, it can happen if `StatusCodes` itself is not properly imported.","error":"TypeError: Cannot read properties of undefined (reading 'OK') at Object.<anonymous>"},{"fix":"Use named imports: `import { StatusCodes } from 'http-status-codes';`.","cause":"Attempting to import a default export (e.g., `import StatusCodes from 'http-status-codes'`) when the library primarily uses named exports. While v2.1.2 added a default export for specific backward compatibility, the primary intended usage is named imports.","error":"TS2305: Module ''http-status-codes'' has no exported member 'default'."}],"ecosystem":"npm"}