{"id":16353,"library":"error-http-response","title":"HTTP Error Response Generator","description":"The `error-http-response` package provides a utility to convert JavaScript `Error` objects into standardized HTTP problem detail objects, adhering to RFC 7807. It's currently stable at version 3.0.1, with major releases primarily driven by updates to the minimal supported Node.js version. The library automatically maps standard error properties like `name`, `message`, and `stack` to the generated HTTP response fields, and allows for extensive customization via `error.http` properties or direct options. It supports both Node.js (>=18.18.0) and browser environments, and ships with TypeScript definitions, making it suitable for modern JavaScript and TypeScript projects that need to consistently format API error responses without manual serialization.","status":"active","version":"3.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/ehmicky/error-http-response","tags":["javascript","api","browser","error","error-handler","error-handling","error-monitoring","http","http-response","typescript"],"install":[{"cmd":"npm install error-http-response","lang":"bash","label":"npm"},{"cmd":"yarn add error-http-response","lang":"bash","label":"yarn"},{"cmd":"pnpm add error-http-response","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is an ES module (ESM) only. CommonJS `require()` is not supported. For TypeScript, ensure your `tsconfig.json` is configured to output ES modules.","wrong":"const errorHttpResponse = require('error-http-response')","symbol":"errorHttpResponse","correct":"import errorHttpResponse from 'error-http-response'"},{"note":"While `AuthError` itself is a user-defined class, its interaction with `errorHttpResponse` is central. No specific import is needed for custom Error classes, but the library relies on standard Error object structure.","symbol":"AuthError","correct":"import errorHttpResponse from 'error-http-response';\n\nclass AuthError extends Error {\n  // ...\n}"}],"quickstart":{"code":"import errorHttpResponse from 'error-http-response';\n\nclass AuthError extends Error {\n  constructor(...args) {\n    super(...args);\n    this.http = {\n      type: 'https://example.com/probs/auth',\n      status: 401,\n    };\n    // Ensure stack trace is captured\n    Error.captureStackTrace(this, AuthError);\n  }\n}\n\nconst error = new AuthError('Could not authenticate.');\nerror.userId = 62;\nObject.assign(error.http, {\n  instance: '/users/62',\n  extra: { userId: 62 },\n});\n\nconst object = errorHttpResponse(error, {\n  extra: { isHttp: true },\n});\n\nconsole.log(object);\n// Expected output:\n// {\n//   type: 'https://example.com/probs/auth',\n//   status: 401,\n//   title: 'AuthError',\n//   detail: 'Could not authenticate.',\n//   instance: '/users/62',\n//   stack: 'AuthError: Could not authenticate.\\n    at ...',\n//   extra: { isHttp: true, userId: 62 }\n// }","lang":"typescript","description":"Demonstrates converting a custom error with additional properties into an RFC 7807-compliant HTTP problem details object, overriding some fields."},"warnings":[{"fix":"Ensure your Node.js environment is version `18.18.0` or higher before upgrading to version 3.x of this package.","message":"The minimal supported Node.js version was upgraded to `18.18.0`.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Ensure your Node.js environment is version `16.17.0` or higher before upgrading to version 2.x of this package.","message":"The minimal supported Node.js version was upgraded to `16.17.0`.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Always use `import errorHttpResponse from 'error-http-response';` to load this package. For older Node.js versions or CommonJS projects, consider using a tool like `esm` or a bundler.","message":"This package is distributed as an ES module (ESM) only. Attempting to `require()` it will result in a runtime error.","severity":"gotcha","affected_versions":"*"},{"fix":"In your `tsconfig.json`, ensure `\"module\"` is set to an ES module target like `\"NodeNext\"` or `\"ESNext\"`.","message":"When using TypeScript, your project must be configured to output ES modules to correctly import and use this package. CommonJS output will lead to module resolution issues.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Change `const errorHttpResponse = require('error-http-response');` to `import errorHttpResponse from 'error-http-response';`.","cause":"Attempted to use `require()` to import `error-http-response`, which is an ES module.","error":"ERR_REQUIRE_ESM: require() of ES Module .../node_modules/error-http-response/build/main.js from ... not supported."},{"fix":"Update your `tsconfig.json` to configure TypeScript to output ES modules, for example, by setting `\"module\": \"NodeNext\"` or `\"module\": \"ESNext\"`.","cause":"TypeScript is configured to output CommonJS modules (e.g., `\"module\": \"CommonJS\"` in `tsconfig.json`), which conflicts with this package's ESM format.","error":"Cannot find module 'error-http-response' or its corresponding type declarations.ts(2307)"}],"ecosystem":"npm"}