Registry / http-networking / http-status-codes

http-status-codes

JSON →
library1.0.3jsnpmunverified

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.

npm install http-status-codes
INSTALL
IMPORT
SIG · HTTP-STATUS-CODES
H
http-status-codes
http-networkingjavascriptv1.0.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

StatusCodes
import { StatusCodes } from 'http-status-codes';
const StatusCodes = require('http-status-codes').StatusCodes;
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.
ReasonPhrases
import { ReasonPhrases } from 'http-status-codes';
const ReasonPhrases = require('http-status-codes').ReasonPhrases;
Provides human-readable reason phrases. Prior to v2.0.0, these were part of a different API (e.g., via `getStatusText`).
getReasonPhrase
import { getReasonPhrase } from 'http-status-codes';
import { getStatusText } from 'http-status-codes';
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.
getStatusCode
import { getStatusCode } from 'http-status-codes';
import { getCode } from 'http-status-codes';
This function was added in v1.4.0. It retrieves a status code from its reason phrase string.

Demonstrates importing and using status code constants, reason phrases, and utility functions to manage HTTP responses.

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.' });
Debug
Known issues
breakingThe function `getStatusText` was renamed to `getReasonPhrase` in version 2.0.0. Code using `getStatusText` will break.
fix
Replace all instances of `getStatusText` with `getReasonPhrase`.
affects: >=2.0.0
breakingThe 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.
fix
Update any logic that checks for the `500` reason phrase to use `ReasonPhrases.INTERNAL_SERVER_ERROR` or the string 'Internal Server Error'.
affects: >=2.0.0
gotchaThe 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.
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.
affects: >=2.0.0
gotchaFrom 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.
fix
Upgrade to `http-status-codes@2.1.4` or newer to benefit from improved tree-shaking and reduced bundle sizes.
affects: <2.1.4
Errors
Common errors & fixes
TypeError: (0, http_status_codes_1.getStatusText) is not a function
Attempting to use the `getStatusText` function from version 1.x with `http-status-codes` version 2.x or newer.
fix
Rename `getStatusText` to `getReasonPhrase`. Example: `import { getReasonPhrase } from 'http-status-codes';`
TypeError: Cannot read properties of undefined (reading 'OK') at Object.<anonymous>
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.
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.
TS2305: Module ''http-status-codes'' has no exported member 'default'.
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.
fix
Use named imports: `import { StatusCodes } from 'http-status-codes';`.
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
http-status-codes — npm install http-status-codes · libregistry