The `errorhandler` middleware is a development-only utility for Express.js applications, designed to display detailed error information during local development. It is currently at version `1.5.2` and has a slow, maintenance-focused release cadence, primarily addressing dependency updates and minor chores. Its core functionality involves robust content negotiation (HTML, JSON, plain text) to present error stack traces and object details to the client when an error occurs. A key differentiator is its explicit intent for development environments, as it exposes sensitive server-side information, making it unsuitable for production use. It handles both standard `Error` objects and generic JavaScript objects, using `util.inspect` for non-Error objects to provide comprehensive debugging insights.
npm install errorhandlerVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to integrate `errorhandler` into a Connect (or Express) application, configuring it to only run in a development environment and providing a custom logging function that sends desktop notifications for errors.
If you rely on console logging in test environments, explicitly set `log: true` in the `errorhandler` options: `app.use(errorhandler({ log: true }))`.Always conditionally enable `errorhandler` based on `process.env.NODE_ENV`. For production, use a more generic error handling middleware that does not expose sensitive information, e.g., `app.use(function (err, req, res, next) { res.status(500).send('Something broke!'); })`.The `log` function should only be used for side-effects like logging to a file, database, or sending notifications. Any response modification must occur *before* the `errorhandler` middleware sends its response.
Always pass actual `Error` objects to `next()` when signaling an error, e.g., `next(new Error('Description'))` or a custom error class extending `Error`. This provides better control over what information is presented.Ensure that response logic in your middleware and routes is robust and does not send multiple responses. If an error occurs, call `next(err)` and let the error handling middleware take over. Avoid `res.send()` or `res.end()` followed by `next(err)` in the same block.
Make sure `app` is initialized as an Express or Connect application, e.g., `const express = require('express'); const app = express();` or `const connect = require('connect'); const app = connect();`.