{"id":18429,"library":"http-custom-errors","title":"http-custom-errors","description":"Custom error classes for all HTTP status codes (4xx and 5xx) in Node.js. Current stable version is 8.0.0, released periodically with low churn. Unlike generic error packages, it provides individual constructors for each status code (e.g., NotFoundError, ForbiddenError) and a factory function createHTTPError(code) that returns the correct error class by numeric status. Errors have code and status properties matching the HTTP status. This library is designed for use with Express.js error-handling middleware. It supports CommonJS only and has no runtime dependencies.","status":"active","version":"8.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/jproulx/node-http-error","tags":["javascript","error","errors","http","status","util"],"install":[{"cmd":"npm install http-custom-errors","lang":"bash","label":"npm"},{"cmd":"yarn add http-custom-errors","lang":"bash","label":"yarn"},{"cmd":"pnpm add http-custom-errors","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"CommonJS only; ESM import will fail.","wrong":"import HTTPErrors from 'http-custom-errors'","symbol":"HTTPErrors","correct":"const HTTPErrors = require('http-custom-errors')"},{"note":"Both destructuring and direct property access work. The example uses the default export.","wrong":"const NotFoundError = require('http-custom-errors').NotFoundError","symbol":"NotFoundError","correct":"const { NotFoundError } = require('http-custom-errors')"},{"note":"Exported as a method on the default export. Can be destructured or called as HTTPErrors.createHTTPError.","wrong":"const createHTTPError = require('http-custom-errors').createHTTPError","symbol":"createHTTPError","correct":"const { createHTTPError } = require('http-custom-errors')"}],"quickstart":{"code":"const HTTPErrors = require('http-custom-errors');\n\ntry {\n  throw new HTTPErrors.NotFoundError('/missing');\n} catch (err) {\n  console.log(err.statusCode); // 404\n  console.log(err.message);     // Not Found\n  console.log(err.code);        // 404\n}\n\nconst err = HTTPErrors.createHTTPError(500);\nconsole.log(err.name);          // InternalServerError\nconsole.log(err.statusCode);    // 500","lang":"javascript","description":"Shows how to throw a 404 error using constructor and how to use createHTTPError factory."},"warnings":[{"fix":"Use require() instead of import.","message":"The library is CommonJS-only; ES module imports will fail at runtime.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use err.code or err.statusCode interchangeably.","message":"Error instances have statusCode and code properties both set to the HTTP status number.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Wrap calls in try/catch or validate the code before calling.","message":"createHTTPError with invalid/unknown status codes returns a generic Error instead of throwing.","severity":"deprecated","affected_versions":">=5.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use const HTTPErrors = require('http-custom-errors');","cause":"Importing the package with ES module syntax (import) returns an empty object in Node.js CommonJS interop.","error":"TypeError: HTTPErrors.createHTTPError is not a function"},{"fix":"Run 'npm install http-custom-errors' and ensure the file is in the same project.","cause":"Package not installed or located in node_modules.","error":"Error: Cannot find module 'http-custom-errors'"},{"fix":"Use new HTTPErrors.NotFoundError(message);","cause":"Attempting to instantiate an error without the 'new' keyword.","error":"HTTPErrors.NotFoundError is not a constructor"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}