Registry / web-framework / httperrors

httperrors

JSON →
library1.0.0jsnpmunverified

The `httperrors` package provides a collection of JavaScript Error classes corresponding to standard HTTP 4xx and 5xx status codes. It allows developers to create instances of these errors either by their numeric status code (e.g., `httpErrors(404)`) or by their UpperCamelCase name (e.g., `new httpErrors.NotFound('Resource not found')`). Each error instance includes the HTTP status code and exposes its CamelCased name as a boolean property, which simplifies error type checking in conditional statements without relying on `instanceof`. Originally designed for use with web frameworks like Express, it facilitates consistent error handling by associating a `statusCode` property with error objects, enabling middleware to set appropriate HTTP response codes. The package is currently at version 2.3.0, with its last update over seven years ago, indicating that it is no longer actively maintained.

npm install httperrors
INSTALL
IMPORT
SIG · HTTPERRORS
H
httperrors
web-frameworkjavascriptv1.0.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.

httpErrors
const httpErrors = require('httperrors');
import httpErrors from 'httperrors';
This package is CommonJS-only and does not support ES module `import` syntax. Attempting to import it in an ESM context will result in an `ERR_REQUIRE_ESM` error.
httpErrors.NotFound
const { NotFound } = require('httperrors'); const notFoundError = new NotFound('Item not found');
import { NotFound } from 'httperrors';
Individual error classes like `NotFound` are properties of the main `httpErrors` object and are accessed via destructuring `require` or `httpErrors.<ClassName>`. Direct ESM imports are not supported.

Demonstrates how to import `httperrors`, instantiate errors by status code and named classes, and perform type-checking in a `try...catch` block.

const httpErrors = require('httperrors'); function handleRequest(resourceExists) { if (!resourceExists) { // Instantiate by status code const notFoundByCode = httpErrors(404, 'The requested resource was not found.'); console.log(`Error (by code): ${notFoundByCode.statusCode} - ${notFoundByCode.message} (Is 404? ${notFoundByCode[404]})`); // Or by name const notFoundByName = new httpErrors.NotFound('The item you are looking for does not exist.'); console.log(`Error (by name): ${notFoundByName.statusCode} - ${notFoundByName.message} (Is NotFound? ${notFoundByName.NotFound})`); throw notFoundByName; } console.log('Resource found and processed.'); } try { handleRequest(false); } catch (err) { if (err.NotFound) { console.error(`Caught NotFound error: ${err.message}. Status: ${err.statusCode}`); } else if (err.BadGateway) { console.error(`Caught BadGateway error: ${err.message}`); } else { console.error(`Caught unexpected error: ${err.name} - ${err.message}`); } } // Example of creating other errors const teapotError = new httpErrors.ImATeapot('I am a teapot, indeed.'); console.log(`Custom error: ${teapotError.message} (Status: ${teapotError.statusCode})`);
Debug
Known issues
breakingThis package is abandoned, with its last release over 7 years ago. It is unlikely to receive updates for critical bug fixes, security vulnerabilities, or compatibility with newer Node.js versions or JavaScript features (like ESM).
fix
Consider migrating to actively maintained alternatives like `http-errors` (note: different package from the same organization) or `http-error-classes`, which offer modern features and better maintenance.
affects: >=2.3.0
gotchaThe package is CommonJS-only and does not natively support ES module `import` syntax. Attempting to use `import httpErrors from 'httperrors';` will lead to runtime errors in a pure ESM environment.
fix
Ensure you are using `const httpErrors = require('httperrors');` in your Node.js application. If you must use ESM, you might need to use dynamic `import()` or explore CommonJS-ESM interoperability solutions, though migrating to a modern, ESM-first HTTP error library is recommended.
affects: >=2.3.0
gotchaThere are no official TypeScript type definitions shipped with this package, nor is there an `@types/httperrors` package.
fix
Developers using TypeScript will need to create their own declaration files (`.d.ts`) for `httperrors` or use a different, type-safe HTTP error library.
affects: >=2.3.0
Errors
Common errors & fixes
TypeError: httpErrors is not a function
This error occurs when attempting to call `httpErrors` as a function after an incorrect ESM import, typically when `httpErrors` resolves to the module object itself instead of the factory function.
fix
Confirm you are using the CommonJS `require` syntax: `const httpErrors = require('httperrors');`. This package is not designed for ESM `import` statements.
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\path\to\node_modules\httperrors\index.js not supported
This error arises when a Node.js project configured as an ES module (`"type": "module"` in `package.json`) attempts to `require()` a CommonJS module, which is the format of `httperrors`.
fix
In an ESM project, you cannot directly `require()` CommonJS modules. Consider using dynamic `import('httperrors')` or, preferably, switch to a modern HTTP error library that provides native ESM support. Alternatively, if your project can be CommonJS, remove `"type": "module"` from your `package.json`.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
createerrorrequiredUsed internally to extend JavaScript Error objects and create the custom HTTP error classes.
Agent activity
6 hits · last 30 days
node
6
Resources
httperrors — npm install httperrors · libregistry