{"id":17704,"library":"httperrors","title":"HTTP Error Classes","description":"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.","status":"abandoned","version":"2.3.0","language":"javascript","source_language":"en","source_url":"git://github.com/One-com/node-httperrors","tags":["javascript","http","errors","error","request","proxy","client","class","errorclass"],"install":[{"cmd":"npm install httperrors","lang":"bash","label":"npm"},{"cmd":"yarn add httperrors","lang":"bash","label":"yarn"},{"cmd":"pnpm add httperrors","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used internally to extend JavaScript Error objects and create the custom HTTP error classes.","package":"createerror","optional":false}],"imports":[{"note":"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.","wrong":"import httpErrors from 'httperrors';","symbol":"httpErrors","correct":"const httpErrors = require('httperrors');"},{"note":"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.","wrong":"import { NotFound } from 'httperrors';","symbol":"httpErrors.NotFound","correct":"const { NotFound } = require('httperrors');\nconst notFoundError = new NotFound('Item not found');"}],"quickstart":{"code":"const httpErrors = require('httperrors');\n\nfunction handleRequest(resourceExists) {\n  if (!resourceExists) {\n    // Instantiate by status code\n    const notFoundByCode = httpErrors(404, 'The requested resource was not found.');\n    console.log(`Error (by code): ${notFoundByCode.statusCode} - ${notFoundByCode.message} (Is 404? ${notFoundByCode[404]})`);\n    // Or by name\n    const notFoundByName = new httpErrors.NotFound('The item you are looking for does not exist.');\n    console.log(`Error (by name): ${notFoundByName.statusCode} - ${notFoundByName.message} (Is NotFound? ${notFoundByName.NotFound})`);\n    throw notFoundByName;\n  }\n  console.log('Resource found and processed.');\n}\n\ntry {\n  handleRequest(false);\n} catch (err) {\n  if (err.NotFound) {\n    console.error(`Caught NotFound error: ${err.message}. Status: ${err.statusCode}`);\n  } else if (err.BadGateway) {\n    console.error(`Caught BadGateway error: ${err.message}`);\n  } else {\n    console.error(`Caught unexpected error: ${err.name} - ${err.message}`);\n  }\n}\n\n// Example of creating other errors\nconst teapotError = new httpErrors.ImATeapot('I am a teapot, indeed.');\nconsole.log(`Custom error: ${teapotError.message} (Status: ${teapotError.statusCode})`);","lang":"javascript","description":"Demonstrates how to import `httperrors`, instantiate errors by status code and named classes, and perform type-checking in a `try...catch` block."},"warnings":[{"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.","message":"This 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).","severity":"breaking","affected_versions":">=2.3.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=2.3.0"},{"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.","message":"There are no official TypeScript type definitions shipped with this package, nor is there an `@types/httperrors` package.","severity":"gotcha","affected_versions":">=2.3.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Confirm you are using the CommonJS `require` syntax: `const httpErrors = require('httperrors');`. This package is not designed for ESM `import` statements.","cause":"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.","error":"TypeError: httpErrors is not a function"},{"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`.","cause":"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`.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module C:\\path\\to\\node_modules\\httperrors\\index.js not supported"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}