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 httperrorsVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to import `httperrors`, instantiate errors by status code and named classes, and perform type-checking in a `try...catch` block.
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.
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.Developers using TypeScript will need to create their own declaration files (`.d.ts`) for `httperrors` or use a different, type-safe HTTP error library.
Confirm you are using the CommonJS `require` syntax: `const httpErrors = require('httperrors');`. This package is not designed for ESM `import` statements.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`.