HTTP Status Codes Constants
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.
Common errors
-
TypeError: (0, http_status_codes_1.getStatusText) is not a function
cause Attempting to use the `getStatusText` function from version 1.x with `http-status-codes` version 2.x or newer.fixRename `getStatusText` to `getReasonPhrase`. Example: `import { getReasonPhrase } from 'http-status-codes';` -
TypeError: Cannot read properties of undefined (reading 'OK') at Object.<anonymous>
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.fixFor 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. -
TS2305: Module ''http-status-codes'' has no exported member 'default'.
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.fixUse named imports: `import { StatusCodes } from 'http-status-codes';`.
Warnings
- breaking The function `getStatusText` was renamed to `getReasonPhrase` in version 2.0.0. Code using `getStatusText` will break.
- breaking 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.
- gotcha 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.
- gotcha 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.
Install
-
npm install http-status-codes -
yarn add http-status-codes -
pnpm add http-status-codes
Imports
- StatusCodes
const StatusCodes = require('http-status-codes').StatusCodes;import { StatusCodes } from 'http-status-codes'; - ReasonPhrases
const ReasonPhrases = require('http-status-codes').ReasonPhrases;import { ReasonPhrases } from 'http-status-codes'; - getReasonPhrase
import { getStatusText } from 'http-status-codes';import { getReasonPhrase } from 'http-status-codes'; - getStatusCode
import { getCode } from 'http-status-codes';import { getStatusCode } from 'http-status-codes';
Quickstart
import { StatusCodes, ReasonPhrases, getReasonPhrase, getStatusCode } from 'http-status-codes';
// Example usage with a hypothetical 'response' object (e.g., from Express)
const response = {
_status: 200,
_data: '',
status(code) {
this._status = code;
return this;
},
send(data) {
this._data = data;
console.log(`Status: ${this._status}, Body: ${JSON.stringify(this._data)}`);
}
};
// Send a successful response
response
.status(StatusCodes.OK)
.send(ReasonPhrases.OK);
// Handle an internal server error
response
.status(StatusCodes.INTERNAL_SERVER_ERROR)
.send({
error: getReasonPhrase(StatusCodes.INTERNAL_SERVER_ERROR)
});
// Get status code from a reason phrase string
const codeFromPhrase = getStatusCode('Bad Request');
response
.status(codeFromPhrase)
.send({
error: 'An unknown error occurred.'
});