HTTP Reason Phrase Lookup

raw JSON →
0.1.0 verified Thu Apr 23 auth: no javascript maintenance

http-reasons is a minimalist JavaScript utility that provides a database to look up human-readable reason phrases for HTTP response status codes. The package is currently at version 0.1.0 and appears to be in a maintenance state, as there have been no significant updates or new releases from Postman Labs, its maintainer, for several years. Its primary function is a static data lookup, offering a simple mapping of numeric HTTP status codes to their corresponding standard reason phrases. Developers seeking more actively maintained alternatives with broader features like explicit ES module support, TypeScript definitions, and more comprehensive status code constants (e.g., `StatusCodes.OK`, `ReasonPhrases.OK`) should consider packages like `http-status-codes`, which offers a more robust and modern API.

error TypeError: httpReasons is not a function
cause Attempting to call the imported `httpReasons` object as a function, rather than accessing its properties.
fix
Access the reason phrase using bracket notation: const reason = httpReasons[200];
error TS2307: Cannot find module 'http-reasons' or its corresponding type declarations.
cause The `http-reasons` package does not include TypeScript type definitions by default.
fix
Install @types/http-reasons if it exists (unlikely for an unmaintained package) or create a manual declaration file, for example, declare module 'http-reasons'; as a quick fix, or provide more specific types if needed.
error ERR_REQUIRE_ESM: require() of ES Module ... not supported
cause Attempting to `require()` an ES Module in a CommonJS context, or vice-versa, when `http-reasons` is likely CJS-only.
fix
Ensure your project's module system is consistent. If using ESM, try import httpReasons from 'http-reasons';. If using CJS, stick to const httpReasons = require('http-reasons');. Consider setting "type": "module" in your package.json for new ESM projects and adjusting imports/exports accordingly.
gotcha The `http-reasons` package is at an early version (0.1.0) and has not been updated in several years by Postman Labs. This indicates a lack of active maintenance, meaning bug fixes, new features, or compatibility updates for newer Node.js versions or standards are unlikely.
fix For projects requiring active maintenance, TypeScript support, or explicit ESM modules, consider alternatives like `http-status-codes`.
breaking This package was developed in a CommonJS-first era. While Node.js offers some interoperability, direct `import` statements in an ES Module context might require specific build configurations or lead to `ERR_REQUIRE_ESM` errors if not handled correctly. It does not explicitly define `exports` in its `package.json`.
fix Use `const httpReasons = require('http-reasons');` for CommonJS projects. For ES Module projects, treat it as a default import (`import httpReasons from 'http-reasons';`) and ensure your build tools (e.g., Webpack, Rollup) are configured to handle CJS dependencies in an ESM context. Or use a native ESM alternative.
gotcha The package does not ship with official TypeScript type definitions (`.d.ts` files). This means TypeScript users will not get type safety or autocompletion for the `httpReasons` object by default, potentially leading to runtime errors if incorrect keys are accessed.
fix Create a custom declaration file (e.g., `types/http-reasons.d.ts`) to provide type definitions for the exported object, or use a typed alternative package.
gotcha The package exports a static object, not a function. Attempting to call `httpReasons(200)` will result in a `TypeError`, as it's designed for direct property access (e.g., `httpReasons[200]`).
fix Access reason phrases using bracket notation for property lookup: `httpReasons[statusCode]`.
npm install http-reasons
yarn add http-reasons
pnpm add http-reasons

Demonstrates importing the http-reasons package and looking up reason phrases for various HTTP status codes, including a non-existent one, and conceptual server usage.

import httpReasons from 'http-reasons';

// Look up the reason phrase for a 200 OK status code
const okReason = httpReasons[200];
console.log(`200: ${okReason}`); // Expected: "OK"

// Look up the reason phrase for a 404 Not Found status code
const notFoundReason = httpReasons[404];
console.log(`404: ${notFoundReason}`); // Expected: "Not Found"

// Attempt to look up a non-existent status code
const nonExistentReason = httpReasons[999];
console.log(`999: ${nonExistentReason}`); // Expected: undefined

// Example of using the lookup in a simple server response (conceptual)
/*
import http from 'http';

http.createServer((req, res) => {
  const statusCode = 200; // or 404, 500, etc.
  const reasonPhrase = httpReasons[statusCode] || 'Unknown Status';
  res.writeHead(statusCode, reasonPhrase, { 'Content-Type': 'text/plain' });
  res.end(`Status ${statusCode}: ${reasonPhrase}`);
}).listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});
*/