Registry / http-networking / error-http-response

error-http-response

JSON →
library3.0.1jsnpmunverified

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.

npm install error-http-response
INSTALL
IMPORT
SIG · ERROR-HTTP-RESPONS
E
error-http-response
http-networkingjavascriptv3.0.1
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

errorHttpResponse
✓ import errorHttpResponse from 'error-http-response'
✗ const errorHttpResponse = require('error-http-response')
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.
AuthError
✓ import errorHttpResponse from 'error-http-response'; class AuthError extends Error { // ... }
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.

Demonstrates converting a custom error with additional properties into an RFC 7807-compliant HTTP problem details object, overriding some fields.

import errorHttpResponse from 'error-http-response'; class AuthError extends Error { constructor(...args) { super(...args); this.http = { type: 'https://example.com/probs/auth', status: 401, }; // Ensure stack trace is captured Error.captureStackTrace(this, AuthError); } } const error = new AuthError('Could not authenticate.'); error.userId = 62; Object.assign(error.http, { instance: '/users/62', extra: { userId: 62 }, }); const object = errorHttpResponse(error, { extra: { isHttp: true }, }); console.log(object); // Expected output: // { // type: 'https://example.com/probs/auth', // status: 401, // title: 'AuthError', // detail: 'Could not authenticate.', // instance: '/users/62', // stack: 'AuthError: Could not authenticate.\n at ...', // extra: { isHttp: true, userId: 62 } // }
Debug
Known issues
breakingThe minimal supported Node.js version was upgraded to `18.18.0`.
fix
Ensure your Node.js environment is version `18.18.0` or higher before upgrading to version 3.x of this package.
affects: >=3.0.0
breakingThe minimal supported Node.js version was upgraded to `16.17.0`.
fix
Ensure your Node.js environment is version `16.17.0` or higher before upgrading to version 2.x of this package.
affects: >=2.0.0
gotchaThis package is distributed as an ES module (ESM) only. Attempting to `require()` it will result in a runtime error.
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.
affects: *
gotchaWhen 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.
fix
In your `tsconfig.json`, ensure `"module"` is set to an ES module target like `"NodeNext"` or `"ESNext"`.
affects: *
Errors
Common errors & fixes
ERR_REQUIRE_ESM: require() of ES Module .../node_modules/error-http-response/build/main.js from ... not supported.
Attempted to use `require()` to import `error-http-response`, which is an ES module.
fix
Change `const errorHttpResponse = require('error-http-response');` to `import errorHttpResponse from 'error-http-response';`.
Cannot find module 'error-http-response' or its corresponding type declarations.ts(2307)
TypeScript is configured to output CommonJS modules (e.g., `"module": "CommonJS"` in `tsconfig.json`), which conflicts with this package's ESM format.
fix
Update your `tsconfig.json` to configure TypeScript to output ES modules, for example, by setting `"module": "NodeNext"` or `"module": "ESNext"`.
Upgrade
Version history
3.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources
error-http-response — npm install error-http-response · libregistry