{"id":17218,"library":"err-http","title":"err-http","description":"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.","status":"abandoned","version":"1.2.2","language":"javascript","source_language":"en","source_url":"git://github.com/aantthony/errors","tags":["javascript","error","constructor","http","404","500","403"],"install":[{"cmd":"npm install err-http","lang":"bash","label":"npm"},{"cmd":"yarn add err-http","lang":"bash","label":"yarn"},{"cmd":"pnpm add err-http","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library is CommonJS-only. Named imports will not work directly in ESM.","wrong":"import { BadRequestError } from 'err-http/badrequest';","symbol":"BadRequestError","correct":"const BadRequestError = require('err-http/badrequest');"},{"note":"Access specific error constructors by requiring their direct path.","wrong":"import UnauthorizedError from 'err-http/unauthorized';","symbol":"UnauthorizedError","correct":"const UnauthorizedError = require('err-http/unauthorized');"},{"note":"The main export is a factory function for creating custom HTTP errors.","wrong":"import createError from 'err-http';","symbol":"err-http factory","correct":"const createError = require('err-http');"}],"quickstart":{"code":"const express = require('express');\nconst app = express();\nconst port = 3000;\n\n// Import specific error constructor for 400 Bad Request\nconst BadRequestError = require('err-http/badrequest');\n\n// Import the factory to create a custom error (e.g., HTTP 418 I'm a Teapot)\nconst CustomTeapotError = require('err-http')('TeapotError', 'I am a teapot', 418);\n\napp.get('/api/resource', (req, res, next) => {\n  // Simulate an invalid request if a required query parameter is missing\n  if (!req.query.id) {\n    return next(new BadRequestError('Missing required query parameter: id.'));\n  }\n  res.json({ message: `Resource with ID ${req.query.id} found.` });\n});\n\napp.get('/api/coffee', (req, res, next) => {\n  // Simulate a custom teapot error for a coffee request\n  next(new CustomTeapotError('Sorry, this server is a teapot and cannot brew coffee.'));\n});\n\n// Generic error handling middleware\napp.use((err, req, res, next) => {\n  console.error(err.stack);\n  // `err-http` errors have a `status` property\n  const status = err.status || 500;\n  const message = err.message || 'Internal Server Error';\n  res.status(status).json({ error: message, code: status });\n});\n\napp.listen(port, () => {\n  console.log(`Server listening at http://localhost:${port}`);\n  console.log('Test with: http://localhost:3000/api/resource'); // Should return 400\n  console.log('Test with: http://localhost:3000/api/resource?id=123'); // Should return 200\n  console.log('Test with: http://localhost:3000/api/coffee'); // Should return 418\n});","lang":"javascript","description":"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."},"warnings":[{"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.","message":"The `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.","severity":"breaking","affected_versions":">=1.0.0"},{"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.","message":"This 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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; }`","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"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');`","cause":"Attempting to use `require()` in an ECMAScript Module (ESM) file, where `require` is not globally available.","error":"ReferenceError: require is not defined"},{"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.","cause":"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.","error":"TypeError: (0, _errHttp.default) is not a function"}],"ecosystem":"npm","meta_description":null}