HTTP Status Codes Map
This package, `builtin-status-codes`, provides a direct mapping of HTTP status codes to their corresponding human-readable messages. It retrieves the latest standard codes directly from Node.js's built-in `http` module when running in a Node.js environment, ensuring accuracy and up-to-date information without external dependencies. For browser environments, it offers a pre-built, zero-dependency version, making it suitable for universal JavaScript applications. The current stable version is 3.0.0. Due to its direct sourcing from Node's core and the stable nature of HTTP status codes, its release cadence is typically infrequent, primarily updating when Node.js introduces new codes or when there are build tool chain updates. Its key differentiator is providing a consistent, minimal API for both Node.js and browser contexts, leveraging built-in capabilities where possible.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use CommonJS `require()` syntax in an ES Module context (e.g., in a file with `"type": "module"` in `package.json` or a `.mjs` file).fixUse `import codes from 'builtin-status-codes'` for ES Modules. -
TypeError: codes is not a function
cause The `codes` export from `builtin-status-codes` is a plain object (a map of numbers to strings), not a callable function.fixAccess status messages using bracket notation with the numeric code, e.g., `codes[200]`. -
TypeError: Cannot read properties of undefined (reading '100')
cause This typically indicates that the `builtin-status-codes` module failed to load correctly, resulting in `codes` being `undefined`, or you're trying to access a non-existent status code in a context where `codes` might be null/undefined.fixEnsure `builtin-status-codes` is correctly installed (`npm install builtin-status-codes`) and imported. Verify that the status code you are trying to access is a valid HTTP status code.
Warnings
- breaking Version 3.0.0 is a major release. While specific breaking changes are not detailed in the provided README, major version bumps typically introduce API changes, updated environment requirements, or different default behaviors. Users upgrading from 2.x.x should consult the package's changelog.
- gotcha The package provides a mapping from numeric HTTP status *codes* (e.g., `200`) to their descriptive *strings* (e.g., 'OK'). It does not offer a direct reverse lookup (string to number) without manually iterating through the map.
- gotcha The browser bundle of `builtin-status-codes` (generated via `npm run build`) is a static snapshot. It might not reflect the absolute latest HTTP status codes as rapidly as the Node.js version, which dynamically queries Node's built-in `http` module at runtime.
Install
-
npm install builtin-status-codes -
yarn add builtin-status-codes -
pnpm add builtin-status-codes
Imports
- codes
import { codes } from 'builtin-status-codes'import codes from 'builtin-status-codes'
- codes
const codes = require('builtin-status-codes') - codes
type HttpStatusCodeMap = Record<number, string>; import codes from 'builtin-status-codes'
Quickstart
import codes from 'builtin-status-codes';
// Get the message for a specific status code
const okMessage = codes[200];
console.log(`200: ${okMessage}`); // Output: 200: OK
const notFoundMessage = codes[404];
console.log(`404: ${notFoundMessage}`); // Output: 404: Not Found
const serverErrorMessage = codes[500];
console.log(`500: ${serverErrorMessage}`); // Output: 500: Internal Server Error
// Iterate over all known status codes and messages
console.log('\nAll available status codes:');
for (const codeStr in codes) {
const code = Number(codeStr);
// Filter out any non-numeric or inherited properties
if (Number.isInteger(code) && code > 0) {
console.log(`${code}: ${codes[code]}`);
}
}