Registry / http-networking / http-constants

http-constants

JSON →
library0.5.0jsnpmunverified

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.

npm install http-constants
INSTALL
IMPORT
SIG · HTTP-CONSTANTS
H
http-constants
http-networkingjavascriptv0.5.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

STATUS_CODES, METHODS
import * as httpConsts from 'http-constants/src/main/consts.js';
import { STATUS_CODES } from 'http-constants';
For ES Modules, the README explicitly directs users to a specific internal path for imports. Direct named imports from the main package entry point might not work as expected in all environments. The `* as` import allows access to all exported constants from that specific file.
http_const
const http_const = require('http-constants');
const http_const = require('http-constants/src/main/consts.js');
For CommonJS environments, the package's main entry point is correctly configured, allowing a straightforward `require()` call to access all constants. Requiring internal paths might bypass transpilation or packaging setup.
HEADER_NAMES
import { HEADER_NAMES } from 'http-constants/src/main/consts.js';
import HEADER_NAMES from 'http-constants/src/main/consts.js';
Individual constants like `HEADER_NAMES` or `HTTP_STATUS_MESSAGES` are exported as named exports from the `consts.js` module for ESM consumption. Attempting a default import will likely result in `undefined`.

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}`);
Debug
Known issues
gotchaThe 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.
affects: >=1.0.0
breakingMajor 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.
affects: >=2.0.0
gotchaThe 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.
affects: >=1.0.0
Errors
Common errors & fixes
ERR_MODULE_NOT_FOUND: Cannot find package 'http-constants' imported from ...
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.
TypeError: require is not a function
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';`.
TypeError: Cannot read properties of undefined (reading 'STATUS_CODES')
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.
Upgrade
Version history
0.5.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
http-constants — npm install http-constants · libregistry