HTTP Methods List
The `methods` package, currently at version 1.1.2, provides a normalized, lower-cased array of HTTP method names supported by the Node.js runtime. This module serves as a robust alternative or enhancement to Node.js core's `http.METHODS`, especially for environments requiring broader compatibility. Its key differentiators include automatically lower-casing all method names for consistent usage and offering a fallback list for older Node.js versions (0.10 and below) that predate the `http.METHODS` export. Furthermore, it's designed to function seamlessly with browser bundling tools like Browserify without inadvertently pulling in the larger `http` shim module, making it lightweight for client-side use cases where a list of standard HTTP verbs is needed. The package is very stable, with infrequent updates, reflecting its foundational role and mature status within the Node.js ecosystem, particularly as a utility for web frameworks like Express.
Common errors
-
TypeError: methods is not a function
cause The imported 'methods' variable is an array, not a function, and was incorrectly invoked as one.fixRemove the function call parentheses: `const allMethods = methods;` -
SyntaxError: Named export 'methods' not found (module 'methods' does not have an export named 'methods')
cause Attempting to use named import syntax (`import { methods } from 'methods'`) for a package that provides only a default export (an array in this case) in a CommonJS context or without proper ESM interoperability configuration.fixUse a default import for ESM: `import methods from 'methods'`, or the CommonJS `require`: `const methods = require('methods')`.
Warnings
- gotcha This package is a CommonJS module and does not officially support native ES Modules (ESM) without a transpiler or bundler. Attempting to use `import { methods } from 'methods'` in a pure ESM environment will fail.
- gotcha The package exports a plain JavaScript array of lower-cased HTTP method strings. It is not an object with named properties, nor is it a function. Attempting to call `methods()` or access `methods.GET` will result in errors.
Install
-
npm install methods -
yarn add methods -
pnpm add methods
Imports
- methods
const methods = require('methods') - methods
import { methods } from 'methods'import methods from 'methods'
- METHODS_ARRAY
const METHODS_ARRAY = require('methods')
Quickstart
const methods = require('methods');
console.log('Supported HTTP methods (lower-cased):');
methods.forEach(method => {
console.log(`- ${method.toUpperCase()}`);
});
// Example: Check if a method is supported
const isSupported = (method) => methods.includes(method.toLowerCase());
console.log('\nIs GET supported?', isSupported('GET'));
console.log('Is TRACE supported?', isSupported('TRACE'));
console.log('Is INVALID supported?', isSupported('INVALID'));