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.
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
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
This package is CommonJS-first and primarily used with `require()`. While Node.js allows `import errorhandler from 'errorhandler'` in ESM contexts, the official examples use `require`.
const errorhandler = require('errorhandler')
The `errorhandler` function itself returns the middleware; it's not a class that needs `new`.
app.use(errorhandler())
The option to provide a custom logging function is named `log`, not `logger`.
app.use(errorhandler({ log: customLogger }))
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.
const connect = require('connect');
const errorhandler = require('errorhandler');
const notifier = require('node-notifier');
const app = connect();
// Assumes NODE_ENV is set by the user, e.g., via 'NODE_ENV=development node app.js'
if (process.env.NODE_ENV === 'development') {
// Only use in development to display detailed error information
app.use(errorhandler({
log: (err, str, req, res) => {
// Custom logging function, e.g., sending system notifications
const title = `Error in ${req.method} ${req.url}`;
notifier.notify({
title: title,
message: str,
// Optionally, add a sound or icon for better visibility
sound: true,
wait: true
});
// Also log to console for standard debugging flow
console.error(str);
}
}));
}
// Example route that intentionally throws an error
app.use('/error', (req, res, next) => {
next(new Error('This is a simulated error for demonstration!'));
});
// Fallback for non-error requests
app.use((req, res, next) => {
res.end('Hello from a non-error path. Try /error to see the error handler in action.');
});
const port = 3000;
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
console.log('Ensure NODE_ENV is set to \'development\' to enable errorhandler.');
});
Upgrade
Version history
Breaking-change detection hasn't run for this library yet.
Audit
Security & dependencies
CVE tracking and dependency tree are planned for a later release.