{"id":17691,"library":"http-constants","title":"HTTP Constants Reference","description":"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.","status":"active","version":"2.1.0-19-gdd89f31","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/aetheric/http-constants","tags":["javascript","http","constants"],"install":[{"cmd":"npm install http-constants","lang":"bash","label":"npm"},{"cmd":"yarn add http-constants","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-constants","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import { STATUS_CODES } from 'http-constants';","symbol":"STATUS_CODES, METHODS","correct":"import * as httpConsts from 'http-constants/src/main/consts.js';"},{"note":"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.","wrong":"const http_const = require('http-constants/src/main/consts.js');","symbol":"http_const","correct":"const http_const = require('http-constants');"},{"note":"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`.","wrong":"import HEADER_NAMES from 'http-constants/src/main/consts.js';","symbol":"HEADER_NAMES","correct":"import { HEADER_NAMES } from 'http-constants/src/main/consts.js';"}],"quickstart":{"code":"import * as httpConstants from 'http-constants/src/main/consts.js';\n\nconsole.log('--- HTTP Status Codes ---');\nfor (const code in httpConstants.STATUS_CODES) {\n  if (Object.prototype.hasOwnProperty.call(httpConstants.STATUS_CODES, code)) {\n    console.log(`Code ${code}: ${httpConstants.STATUS_CODES[code]}`);\n    if (parseInt(code) >= 400 && parseInt(code) < 500) {\n      console.log(`  (Client Error)`);\n    }\n  }\n}\n\nconsole.log('\\n--- Common HTTP Headers ---');\nhttpConstants.HEADER_NAMES.forEach(header => {\n  console.log(`Header: ${header}`);\n});\n\nconsole.log('\\n--- HTTP Methods ---');\nhttpConstants.METHODS.forEach(method => {\n  console.log(`Method: ${method}`);\n});\n\n// Example of accessing a specific constant\nconst unauthorizedCode = httpConstants.HTTP_STATUS_CODES.UNAUTHORIZED;\nconst unauthorizedMessage = httpConstants.STATUS_CODES[unauthorizedCode];\nconsole.log(`\\nUnauthorized (Code ${unauthorizedCode}): ${unauthorizedMessage}`);","lang":"typescript","description":"Demonstrates importing all constants using ES Modules and accessing various HTTP status codes, header names, and methods."},"warnings":[{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Review the package's internal structure if you were relying on non-public APIs or paths. Adhere to the documented import methods.","message":"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.","severity":"breaking","affected_versions":">=2.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"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.","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.","error":"ERR_MODULE_NOT_FOUND: Cannot find package 'http-constants' imported from ..."},{"fix":"Refactor your code to use ES Module `import` syntax: `import * as httpConsts from 'http-constants/src/main/consts.js';`.","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`).","error":"TypeError: require is not a function"},{"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.","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.","error":"TypeError: Cannot read properties of undefined (reading 'STATUS_CODES')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}