{"id":16381,"library":"http-methods-constants","title":"HTTP Methods Constants","description":"http-methods-constants is a lightweight utility package that provides standard HTTP method verbs as string constants. Its primary purpose is to eliminate \"magic strings\" in codebases, improving readability and reducing potential typos when referring to methods like 'GET', 'POST', 'PUT', or 'DELETE'. The current stable version is 1.1.0. Given its fundamental nature, it is expected to have a very stable and infrequent release cadence, with new versions primarily occurring for minor improvements or ecosystem compatibility updates rather than new features. It differentiates itself through its extreme simplicity and singular focus, offering a straightforward object of string constants without additional logic or validation, making it an ideal drop-in for projects needing explicit HTTP method references.","status":"active","version":"1.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/avivharuzi/http-methods-constants","tags":["javascript","http","node","methods","constants"],"install":[{"cmd":"npm install http-methods-constants","lang":"bash","label":"npm"},{"cmd":"yarn add http-methods-constants","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-methods-constants","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package exports a single default object containing all methods. Named imports are not supported.","wrong":"import { GET } from 'http-methods-constants';","symbol":"httpMethods","correct":"const httpMethods = require('http-methods-constants');"},{"note":"Method names are uppercase strings (e.g., 'GET'). Access them as properties of the imported object.","wrong":"import httpMethods from 'http-methods-constants';\nconsole.log(httpMethods['get']);","symbol":"httpMethods.GET","correct":"const httpMethods = require('http-methods-constants');\nconsole.log(httpMethods.GET);"},{"note":"While primarily a CommonJS module, modern bundlers and Node.js environments can import the default export as a module.","wrong":"import * as httpMethods from 'http-methods-constants';","symbol":"httpMethods (ESM)","correct":"import httpMethods from 'http-methods-constants';"}],"quickstart":{"code":"const httpMethods = require('http-methods-constants');\n\nconsole.log(\"All supported HTTP methods:\");\nfor (const methodKey in httpMethods) {\n  if (Object.prototype.hasOwnProperty.call(httpMethods, methodKey)) {\n    console.log(`- ${methodKey}: ${httpMethods[methodKey]}`);\n  }\n}\n\n// Example usage in an Express.js-like route handler (conceptual)\nfunction handleRequest(method, path, body) {\n  if (method === httpMethods.GET && path === '/api/data') {\n    console.log(`Handling GET request for ${path}`);\n    return { status: 200, data: 'Some data' };\n  } else if (method === httpMethods.POST && path === '/api/items') {\n    console.log(`Handling POST request for ${path} with body:`, body);\n    return { status: 201, message: 'Item created' };\n  }\n  return { status: 404, message: 'Not Found' };\n}\n\nconsole.log(handleRequest('GET', '/api/data'));\nconsole.log(handleRequest('POST', '/api/items', { name: 'New Item' }));\n","lang":"javascript","description":"Demonstrates importing the constants object and iterating through all available HTTP methods, followed by a conceptual example of using them in a request handler for clarity."},"warnings":[{"fix":"Use a dedicated HTTP framework (e.g., Express, Koa) or a request library (e.g., Axios, Node's built-in `http` module) for actual HTTP request handling and validation.","message":"This package only provides string constants for HTTP methods. It does not include any validation logic, parsing capabilities, or integration with HTTP servers/clients. It is purely for referencing method names.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always import the full object: `const httpMethods = require('http-methods-constants');` for CommonJS or `import httpMethods from 'http-methods-constants';` for ESM.","message":"The package currently appears to be primarily a CommonJS module. While bundlers and Node.js can often handle `import httpMethods from 'http-methods-constants';` for CJS default exports, direct named ESM imports (e.g., `import { GET } from 'http-methods-constants';`) are not supported.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure you are importing the full object: `const httpMethods = require('http-methods-constants');` in CommonJS or `import httpMethods from 'http-methods-constants';` in ESM. Then access methods as properties: `httpMethods.GET`.","cause":"Incorrect import of the http-methods-constants module, often due to attempting named imports or failing to correctly assign the default export.","error":"TypeError: Cannot read properties of undefined (reading 'GET') or similar 'is not a function' errors"},{"fix":"Run `npm install http-methods-constants` to ensure the package is in your `node_modules` directory. Verify the import statement is `require('http-methods-constants')` or `import httpMethods from 'http-methods-constants'`.","cause":"The package was not installed, or the import/require path is incorrect.","error":"ERR_MODULE_NOT_FOUND or MODULE_NOT_FOUND"}],"ecosystem":"npm"}