HTTP Constants Reference

raw JSON →
2.1.0-19-gdd89f31 verified Thu Apr 23 auth: no javascript

The `http-constants` package provides a centralized collection of standard HTTP constant values, including status codes, common header names, and method verbs. It aims to offer a consistent and readily available source for these values, preventing 'magic string' issues and enhancing code readability and maintainability in Node.js and browser environments. The package is currently stable at version 2.2.0 (as of its 'Monstrous Agenda' release) and appears to follow an as-needed release cadence rather than a strict schedule, with new versions addressing community requests like additional types. Key differentiators include its explicit support for both ES Modules via specific paths and CommonJS, alongside providing a comprehensive set of constants that might otherwise be scattered or inconsistently defined across different libraries or codebases.

error ERR_MODULE_NOT_FOUND: Cannot find package 'http-constants' imported from ...
cause Attempting to `import` `http-constants` in an ESM context without specifying the full path `/src/main/consts.js`, or using `require()` in an ESM-only environment.
fix
For ESM, change import ... from 'http-constants' to import * as httpConsts from 'http-constants/src/main/consts.js'. For CJS, ensure you are using const http_const = require('http-constants') and your environment supports CommonJS.
error TypeError: require is not a function
cause You are attempting to use `require()` within an ES Module (ESM) file or in a Node.js environment configured for ESM (e.g., `"type": "module"` in `package.json`).
fix
Refactor your code to use ES Module import syntax: import * as httpConsts from 'http-constants/src/main/consts.js';.
error TypeError: Cannot read properties of undefined (reading 'STATUS_CODES')
cause This usually happens when an import fails to resolve correctly, leading to `httpConsts` being `undefined` or an empty object, especially if using a wrong import path for ESM.
fix
Verify that your import statement matches the required format for your module system: import * as httpConsts from 'http-constants/src/main/consts.js'; for ESM, or const httpConsts = require('http-constants'); for CJS.
gotcha The package requires different import paths for ES Modules (ESM) and CommonJS (CJS). Using the CJS `require('http-constants')` in an ESM context or the ESM `import ... from 'http-constants/src/main/consts.js'` in a CJS context will lead to module resolution errors or undefined exports.
fix Ensure you use `import * as name from 'http-constants/src/main/consts.js'` for ESM and `const name = require('http-constants')` for CJS environments.
breaking Major version 2.0.0 ('Wandering Establishment') introduced potential internal changes that might affect deeply nested or non-standard imports. While the core API (`require('http-constants')` or `import .../consts.js`) remains consistent, custom build pipelines or direct file system access to internal package structure might require updates.
fix Review the package's internal structure if you were relying on non-public APIs or paths. Adhere to the documented import methods.
gotcha The ESM import path `http-constants/src/main/consts.js` is non-standard, pointing directly to a source file rather than a packaged `dist` file or a module entry point defined in `package.json`'s `exports` field. This might cause issues with certain bundlers, TypeScript configurations, or future package updates that alter the internal file structure.
fix Configure your bundler (e.g., Webpack, Rollup, Parcel) or TypeScript compiler (`tsconfig.json`) to correctly resolve or allow imports from `src/`. Be prepared for potential adjustments in future major versions if the internal path changes.
npm install http-constants
yarn add http-constants
pnpm add http-constants

Demonstrates importing all constants using ES Modules and accessing various HTTP status codes, header names, and methods.

import * as httpConstants from 'http-constants/src/main/consts.js';

console.log('--- HTTP Status Codes ---');
for (const code in httpConstants.STATUS_CODES) {
  if (Object.prototype.hasOwnProperty.call(httpConstants.STATUS_CODES, code)) {
    console.log(`Code ${code}: ${httpConstants.STATUS_CODES[code]}`);
    if (parseInt(code) >= 400 && parseInt(code) < 500) {
      console.log(`  (Client Error)`);
    }
  }
}

console.log('\n--- Common HTTP Headers ---');
httpConstants.HEADER_NAMES.forEach(header => {
  console.log(`Header: ${header}`);
});

console.log('\n--- HTTP Methods ---');
httpConstants.METHODS.forEach(method => {
  console.log(`Method: ${method}`);
});

// Example of accessing a specific constant
const unauthorizedCode = httpConstants.HTTP_STATUS_CODES.UNAUTHORIZED;
const unauthorizedMessage = httpConstants.STATUS_CODES[unauthorizedCode];
console.log(`\nUnauthorized (Code ${unauthorizedCode}): ${unauthorizedMessage}`);