{"id":17837,"library":"node-http-error","title":"Node.js HTTP Error Handling","description":"node-http-error provides a simple mechanism for creating and throwing HTTP-specific errors within Node.js applications, particularly useful for API development with frameworks like Express. This package allows developers to instantiate errors with a given HTTP status code, an optional custom message, and additional properties, while also automatically generating default messages for standard status codes. Key features of version 2.0.0, released in 2014, include full stack traces, the ability to omit the `new` operator when creating instances, and direct access to `status` and `statusCode` properties for integration with error handling middleware. However, this specific package by carsondarling is considered abandoned, with its last update occurring nine years ago. Developers are strongly encouraged to use more actively maintained alternatives like `http-errors` (from `jshttp`) for modern Node.js projects.","status":"abandoned","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/carsondarling/node-http-error","tags":["javascript","error","status code","http"],"install":[{"cmd":"npm install node-http-error","lang":"bash","label":"npm"},{"cmd":"yarn add node-http-error","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-http-error","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package (v2.0.0) is CommonJS only and does not provide ES Module exports. Attempting an ESM import will result in a runtime error.","wrong":"import { HTTPError } from 'node-http-error';","symbol":"HTTPError","correct":"const HTTPError = require('node-http-error');"},{"note":"While `new HTTPError()` works, the library explicitly supports calling `HTTPError()` as a function without `new` for convenience and brevity, as shown in the original documentation.","wrong":"next(new HTTPError(500, 'Error by design.'));","symbol":"HTTPError (function call)","correct":"next(HTTPError(500, 'Error by design.'));"}],"quickstart":{"code":"const HTTPError = require('node-http-error');\nconst express = require('express');\nconst app = express();\n\napp.get('/error', function(req, res, next) {\n  // Example of throwing a 500 Internal Server Error with a custom message\n  return next(new HTTPError(500, 'Error by design.'));\n});\n\napp.get('/not-found', function(req, res, next) {\n  // Example of throwing a 404 Not Found error using only the status code\n  return next(HTTPError(404)); \n});\n\n// Error handler middleware\napp.use(function(err, req, res, next) {\n  if (err instanceof HTTPError) {\n    res.status(err.status || err.statusCode || 500);\n    res.send(err.message || 'An unexpected error occurred.');\n  } else {\n    res.status(500).send('Unhandled server error.');\n  }\n});\n\napp.listen(3000, () => {\n  console.log('Server listening on http://localhost:3000');\n  console.log('Try visiting http://localhost:3000/error');\n  console.log('Try visiting http://localhost:3000/not-found');\n});","lang":"javascript","description":"This quickstart demonstrates how to create and throw `HTTPError` instances within an Express.js application, and how to set up a basic error handling middleware to catch and respond to these errors using their status code and message."},"warnings":[{"fix":"Migrate to a currently maintained alternative like `http-errors` (from `jshttp`) or `@curveball/http-errors`, which offer similar functionality with active development and modern JavaScript support.","message":"The `node-http-error` package (by carsondarling) has been abandoned since 2014. It receives no updates, bug fixes, or security patches, making it unsuitable for new projects or production environments.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"For ESM projects, use an actively maintained package that provides ESM compatibility. If you must use this package in an ESM context, consider dynamic `import()` or transpilation, but migration is strongly recommended.","message":"This package is CommonJS-only. It cannot be directly imported into ES Module (ESM) contexts using `import` statements, leading to `ReferenceError: require is not defined`.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Upgrade to a modern HTTP error utility that is actively tested and maintained with current Node.js versions.","message":"The package's reliance on older Node.js HTTP module behavior might be incompatible with stricter HTTP parser changes introduced in Node.js v12 and later, potentially leading to unexpected error handling or server behavior.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"This package is CommonJS-only. Either refactor your project to use CommonJS, use dynamic `import()` (if compatible with your setup), or, preferably, migrate to a modern, actively maintained HTTP error library that supports ESM.","cause":"Attempting to use `require()` in an ES Module (ESM) context without a CommonJS compatibility layer.","error":"ReferenceError: require is not defined"},{"fix":"Ensure `require('node-http-error')` correctly resolves and that `HTTPError` is accessible in your scope. Verify file paths and npm installation. Given the package's age, ensure your Node.js environment is not causing compatibility issues during module loading.","cause":"The `HTTPError` object (or the `node-http-error` module itself) failed to load or was incorrectly referenced, resulting in `undefined` being passed where a function was expected.","error":"TypeError: app.use() requires a middleware function but got a undefined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}