HTTP Constants Reference
raw JSON →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.
Common errors
error ERR_MODULE_NOT_FOUND: Cannot find package 'http-constants' imported from ... ↓
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 ↓
import syntax: import * as httpConsts from 'http-constants/src/main/consts.js';. error TypeError: Cannot read properties of undefined (reading 'STATUS_CODES') ↓
import * as httpConsts from 'http-constants/src/main/consts.js'; for ESM, or const httpConsts = require('http-constants'); for CJS. Warnings
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. ↓
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. ↓
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. ↓
Install
npm install http-constants yarn add http-constants pnpm add http-constants Imports
- STATUS_CODES, METHODS wrong
import { STATUS_CODES } from 'http-constants';correctimport * as httpConsts from 'http-constants/src/main/consts.js'; - http_const wrong
const http_const = require('http-constants/src/main/consts.js');correctconst http_const = require('http-constants'); - HEADER_NAMES wrong
import HEADER_NAMES from 'http-constants/src/main/consts.js';correctimport { HEADER_NAMES } from 'http-constants/src/main/consts.js';
Quickstart
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}`);