Registry / http-networking / err-http

err-http

JSON →
library1.2.2jsnpmunverified

The `err-http` package provides a collection of error constructors specifically designed for common HTTP status codes, simplifying error management in web applications. It allows developers to easily create custom error types that inherit from the standard `Error` object and automatically carry an associated HTTP status code, such as `BadRequestError` (400) or a custom `TeapotError` (418). Currently stable at version 1.2.2, the library has not seen significant updates or new major versions since 2014, indicating an abandoned state. Its core value lies in its straightforward approach to mapping application errors to HTTP semantics, enabling clearer API responses. However, its CommonJS-only nature and lack of active maintenance pose challenges for modern JavaScript development practices, particularly within ESM-first projects or those relying on TypeScript for type safety. This package serves as a foundational example of HTTP error handling but is largely superseded by more actively maintained alternatives.

npm install err-http
INSTALL
IMPORT
SIG · ERR-HTTP
E
err-http
http-networkingjavascriptv1.2.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

BadRequestError
const BadRequestError = require('err-http/badrequest');
import { BadRequestError } from 'err-http/badrequest';
The library is CommonJS-only. Named imports will not work directly in ESM.
UnauthorizedError
const UnauthorizedError = require('err-http/unauthorized');
import UnauthorizedError from 'err-http/unauthorized';
Access specific error constructors by requiring their direct path.
err-http factory
const createError = require('err-http');
import createError from 'err-http';
The main export is a factory function for creating custom HTTP errors.

This quickstart demonstrates creating and using pre-defined `err-http` errors (like BadRequestError) and custom errors (like TeapotError) within an Express application, showing how error handling middleware can leverage the `status` property of these errors.

const express = require('express'); const app = express(); const port = 3000; // Import specific error constructor for 400 Bad Request const BadRequestError = require('err-http/badrequest'); // Import the factory to create a custom error (e.g., HTTP 418 I'm a Teapot) const CustomTeapotError = require('err-http')('TeapotError', 'I am a teapot', 418); app.get('/api/resource', (req, res, next) => { // Simulate an invalid request if a required query parameter is missing if (!req.query.id) { return next(new BadRequestError('Missing required query parameter: id.')); } res.json({ message: `Resource with ID ${req.query.id} found.` }); }); app.get('/api/coffee', (req, res, next) => { // Simulate a custom teapot error for a coffee request next(new CustomTeapotError('Sorry, this server is a teapot and cannot brew coffee.')); }); // Generic error handling middleware app.use((err, req, res, next) => { console.error(err.stack); // `err-http` errors have a `status` property const status = err.status || 500; const message = err.message || 'Internal Server Error'; res.status(status).json({ error: message, code: status }); }); app.listen(port, () => { console.log(`Server listening at http://localhost:${port}`); console.log('Test with: http://localhost:3000/api/resource'); // Should return 400 console.log('Test with: http://localhost:3000/api/resource?id=123'); // Should return 200 console.log('Test with: http://localhost:3000/api/coffee'); // Should return 418 });
Debug
Known issues
breakingThe `err-http` package has not been updated since 2014, making it abandoned. This implies no new features, bug fixes, or security patches will be released. Modern JavaScript projects should consider more actively maintained alternatives for HTTP error handling.
fix
Migrate to a currently maintained HTTP error library, such as `http-errors` or custom error classes that explicitly set a status property, especially for new projects or applications requiring ongoing security and feature support.
affects: >=1.0.0
gotchaThis library is exclusively CommonJS (`require`). Direct ESM `import` statements (e.g., `import { BadRequestError } from 'err-http/badrequest';`) will fail in pure ESM environments or result in unexpected behavior when transpiled, as it does not provide explicit ESM exports.
fix
For ESM projects, you must use a CommonJS interop solution like `import { createRequire } from 'module'; const require = createRequire(import.meta.url); const BadRequestError = require('err-http/badrequest');` or configure your build system to handle CJS imports correctly.
affects: >=1.0.0
gotchaThe library does not provide TypeScript type definitions. Projects using TypeScript will lack type safety when interacting with `err-http` constructors and custom errors, requiring manual type declarations or `@ts-ignore` directives.
fix
Create a `d.ts` declaration file (e.g., `err-http.d.ts`) to define the module's types manually. For example: `declare module 'err-http/badrequest' { class BadRequestError extends Error { status: number; constructor(message?: string); } export = BadRequestError; }`
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to use `require()` in an ECMAScript Module (ESM) file, where `require` is not globally available.
fix
If within an ESM file, you must use `import` statements or explicitly create a `require` function for CJS interop: `import { createRequire } from 'module'; const require = createRequire(import.meta.url); const MyError = require('err-http/myerror');`
TypeError: (0, _errHttp.default) is not a function
Incorrect default import syntax or incorrect assumption about the module's default export when transpiling or using a non-standard module loader. The main `err-http` module exports a function directly, not an object with a default property.
fix
In CommonJS, use `const createError = require('err-http');`. In ESM with CJS interop, you might need `import createError from 'err-http';` or `import * as ErrHttp from 'err-http'; const createError = ErrHttp;` if transpilers wrap it, but it's fundamentally a CommonJS module with a direct function export.
Upgrade
Version history
1.2.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
err-http — npm install err-http · libregistry