HTTP Status Map
This package provides a comprehensive, TypeScript-first solution for working with HTTP status codes in JavaScript and Node.js applications. It offers a structured approach to access HTTP status code constants, retrieve human-readable names, and categorize codes into their respective classes (e.g., informational, success, client error, server error). The current stable version is 1.1.3, released in December 2022. While its release cadence has been infrequent, focusing primarily on bug fixes and minor dependency updates, the library remains a reliable choice for static HTTP status code mapping. Key differentiators include its robust TypeScript typings, which ensure compile-time safety and better developer experience, and its utility functions that simplify common operations like checking status categories, providing an abstract layer over raw numerical comparisons. It serves as a lightweight and focused alternative to manually managing HTTP status code logic.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'OK')
cause Attempting to destructure `NHttpStatuses` (or other named exports) from a CommonJS `require` call using incorrect syntax, often `const NHttpStatuses = require('http-response-status').NHttpStatuses` instead of correct named destructuring.fixFor CommonJS, use `const { NHttpStatuses } = require('http-response-status');`. Alternatively, switch your project to ES Modules and use `import { NHttpStatuses } from 'http-response-status';`. -
SyntaxError: Named export 'getHttpStatusName' not found. The requested module 'http-response-status' is a CommonJS module, which may not support named exports.
cause This error occurs when a bundler or runtime attempts to use ESM named `import` syntax (`import { getHttpStatusName } from '...'`) for a module it incorrectly identifies as purely CommonJS, or when the `package.json` `exports` field isn't correctly configured for dual-package usage in a mixed environment.fixVerify your project's module type configuration (`"type": "module"` in `package.json` for ESM) and your bundler settings. If targeting older Node.js or CJS, use `const { getHttpStatusName } = require('http-response-status');`.
Warnings
- gotcha As of version 1.1.3, internal module paths were updated in the `package.json`. While this change typically improves module resolution in modern build systems, projects relying on older or non-standard configurations that directly referenced internal file paths (e.g., `http-response-status/dist/index.js`) might experience resolution issues or unexpected errors.
- gotcha The package primarily uses ES Modules (ESM) for its exports, alongside CommonJS compatibility. Mixing ESM `import` statements with incorrect CommonJS `require()` syntax for named exports can lead to `TypeError` (e.g., 'X is undefined') or `SyntaxError` (e.g., 'Named export not found').
- gotcha This library is a static map of HTTP response statuses and utility functions based on standard specifications. It does not dynamically fetch or update status definitions from external sources, nor does it provide HTTP client or server functionality. Its scope is strictly limited to providing programmatic access to static HTTP status metadata.
Install
-
npm install http-response-status -
yarn add http-response-status -
pnpm add http-response-status
Imports
- NHttpStatuses
const NHttpStatuses = require('http-response-status').NHttpStatusesimport { NHttpStatuses } from 'http-response-status' - getHttpStatusName
import getHttpStatusName from 'http-response-status'
import { getHttpStatusName } from 'http-response-status' - getHttpStatusCategory
import { getHttpStatusCategory } from 'http-response-status' - HTTP_STATUS_SUCCESS
const HTTP_STATUS_SUCCESS = require('http-response-status').HTTP_STATUS_SUCCESSimport { HTTP_STATUS_SUCCESS } from 'http-response-status'
Quickstart
import { NHttpStatuses, getHttpStatusName, getHttpStatusCategory, HTTP_STATUS_SUCCESS, HTTP_STATUS_CLIENT_ERROR } from 'http-response-status';
function processHttpResponse(statusCode: number): string {
if (statusCode === NHttpStatuses.OK) {
console.log(`Status ${statusCode}: All good, operation successful!`);
return 'OK';
}
const statusName = getHttpStatusName(statusCode);
if (statusName) {
console.log(`Status ${statusCode}: ${statusName}`);
} else {
console.log(`Status ${statusCode}: Unknown Status Code`);
}
const statusCategory = getHttpStatusCategory(statusCode);
if (statusCategory === HTTP_STATUS_SUCCESS) {
console.log(`Category for ${statusCode} is Success.`);
return 'Success';
} else if (statusCategory === HTTP_STATUS_CLIENT_ERROR) {
console.log(`Category for ${statusCode} is Client Error.`);
return 'Client Error';
} else {
console.log(`Category for ${statusCode} is ${statusCategory || 'Other'}.`);
return 'Other';
}
}
processHttpResponse(200);
processHttpResponse(404);
processHttpResponse(500);
processHttpResponse(201);
processHttpResponse(1000); // Example of an unknown status code