HTTP Status Code Map
This `http-codes` package provides a straightforward JavaScript object that maps common HTTP status message constants (e.g., `OK`, `NOT_FOUND`) to their corresponding numeric codes (e.g., `200`, `404`). Originally released as version `1.0.0` in 2014, it explicitly based its mapping on the built-in HTTP status codes available in Node.js `0.10.22`. The package emphasizes simplicity and a lack of external dependencies, aiming to automatically incorporate updates from Node.js's internal HTTP module, although it has not received any updates since its initial release. Due to its age and lack of maintenance, it does not include many modern HTTP status codes and is primarily suitable for legacy Node.js environments or projects that specifically require the exact mapping from Node.js `0.10.22`.
Common errors
-
SyntaxError: Named export 'OK' not found. The requested module 'http-codes' does not provide an export named 'OK'
cause Attempting to use ES module named import syntax (`import { OK } from 'http-codes';`) with a CommonJS-only package.fixUse CommonJS `require()` syntax: `const { OK } = require('http-codes');` or `const httpCodes = require('http-codes'); const okCode = httpCodes.OK;` -
Error [ERR_REQUIRE_ESM]: require() of ES Module ... from ... not supported.
cause Attempting to `require()` a package in an ESM context that does not provide a CommonJS entry point or is explicitly ESM-only. (This package is CJS, but this error is common for developers trying to 'require' in ESM projects)fixWhile `http-codes` *is* CJS, this error often appears when attempting to use CJS-only packages in an ESM project without proper configuration. For `http-codes`, ensure your project context is CJS, or if ESM, use dynamic `import()`: `const httpCodes = await import('http-codes');` (though direct object access might need `httpCodes.default` depending on interop). Better yet, use a modern alternative.
Warnings
- breaking This package is extremely old, published in 2014, and specifically based on Node.js 0.10.22. It is not compatible with modern JavaScript module systems (ESM) without explicit CommonJS interop.
- gotcha The package does not contain newer HTTP status codes defined after 2014 (e.g., 421 Misdirected Request, 425 Too Early, 451 Unavailable For Legal Reasons, 103 Early Hints). Relying on this package for a comprehensive list of modern codes will lead to omissions.
- gotcha The package exports a plain object directly. There are no functions to get status messages from codes, or to parse messages, which are common features in more modern HTTP status code libraries.
Install
-
npm install http-codes -
yarn add http-codes -
pnpm add http-codes
Imports
- httpCodes
import httpCodes from 'http-codes';
const httpCodes = require('http-codes'); - OK
import { OK } from 'http-codes';const { OK, NOT_FOUND } = require('http-codes');
Quickstart
const httpCodes = require('http-codes');
console.log('HTTP Status Codes Map:');
console.log(httpCodes);
// Access a specific status code
const okCode = httpCodes.OK;
console.log(`\nOK (200): ${okCode}`);
const notFoundCode = httpCodes.NOT_FOUND;
console.log(`NOT_FOUND (404): ${notFoundCode}`);
const imATeapotCode = httpCodes.IM_A_TEAPOT;
console.log(`IM_A_TEAPOT (418): ${imATeapotCode}`);
// Example usage in a simple server (requires Node.js http module)
const http = require('http');
const server = http.createServer((req, res) => {
if (req.url === '/success') {
res.writeHead(httpCodes.OK, { 'Content-Type': 'text/plain' });
res.end('Request successful!');
} else if (req.url === '/not-found') {
res.writeHead(httpCodes.NOT_FOUND, { 'Content-Type': 'text/plain' });
res.end('Resource not found.');
} else {
res.writeHead(httpCodes.IM_A_TEAPOT, { 'Content-Type': 'text/plain' });
res.end('I\u0027m a teapot.');
}
});
server.listen(3000, () => {
console.log('\nServer listening on http://localhost:3000');
console.log('Try visiting /success, /not-found, or any other path.');
});