express-error-toolkit โ the One NPM Package to Handle All Express Errors


As a full-stack developer, Iโve spent countless hours building robust backend applications with Express.js. While Express is lightweight and unopinionated โ exactly what makes it great โ error handling has always been repetitive and tedious.
I was tired of copy-pasting the same boilerplate across projects:
- โ
Writing
notFoundHandler
manually - โ
Writing
globalErrorHandler
for every single project - โ
Wrapping every async function in a try-catch or
asyncHandler
- โ
Creating
CustomApiError
,NotFoundError
,BadrequestError
and other custom errors. - โ
Installing separate libraries like
chalk
just to make my logs more readable
Even after using popular libraries like express-async-errors
or async-error-handler
, I still had to manually wire up custom errors and error-handling middleware. And none of them improved the developer experience in the terminal.
So, I built - ๐ express-error-toolkit โ the one package to handle all Express errors.
๐งฐ What's Included?
This package provides everything you need to handle errors in a production-grade Express app:
- โ
Type-safe custom error classes (
NotFoundError
,BadRequestError
, etc.) - โ
Drop-in middleware:
globalErrorHandler
,notFoundHandler
- โ
A clean
asyncHandler
utility - โ
A
httpError()
function for custom one-liners - โ
Colorful and readable console logs โ no need for
chalk
or similar - โ
Flexible
.env
or programmatic configuration - โ Built-in support for CommonJS and ESM
- โ Designed with DX in mind
๐ Build Process
The package is written entirely in TypeScript and built with:
- tsup โ lightning-fast bundler
- http-status-toolkit โ my own utility, to handle http-status-codes in more human readable way
- ANSI escape codes โ to create colorful terminal output (no dependencies required)
The idea was to bundle everything in a single utility that you can drop into any Express project, and instantly get a polished, configurable, DX-optimized error system.
โก Installation
npm install express-error-toolkit
โน๏ธ Requires Node.js version
>=14
Make sure you have
express
installed in your project.
๐ฆ Usage Examples
1. Wrap async route handlers
import { asyncHandler } from 'express-error-toolkit';
app.get('/users/:id', asyncHandler(async (req, res) =>
{throw new Error('User not found'); }) );
2. Use custom errors
import { NotFoundError } from 'express-error-toolkit';
app.get('/user/:id', (req, res) =>
{throw new NotFoundError('User not found'); });
3. Catch unregistered routes
import { notFoundHandler } from 'express-error-toolkit';
app.use(notFoundHandler);
4. Global error handler
import { globalErrorHandler } from 'express-error-toolkit';
app.use(globalErrorHandler);
By default, it includes stack trace and logs the error, but removes both when NODE_ENV=production
.
๐งช Console Output โ Traffic Light Theme
To make debugging more intuitive, express-error-toolkit
uses ANSI escape codes to show:
๐ด Error Message in bold red
๐ก Error Details in bold yellow
๐ข Stack Trace in dim green, line-by-line
๐ธ Console Preview:
Colors are styled without external libraries โ just pure ANSI codes
๐ ๏ธ Optional Configuration
5. Set Options Globally (Optional)
You can configure the error handling behavior (e.g., hide stack traces, configuring intro line in console, and disable console logging even in development) using either:
โ
Via && .env
**:
SHOW_STACK=false LOG_ERROR=false
โ Or directly in your code:
import { setErrorOptions } from 'express-error-toolkit';
setErrorOptions({showStack: false, logError: false, introLine: false });
This overrides the default behavior (based on NODE_ENV
or .env
file).
๐ Utility Helpers
import { httpError, StatusCodes } from 'express-error-toolkit';
throw httpError('Something broke!', StatusCodes.BAD_REQUEST);
import { isCustomAPIError } from 'express-error-toolkit';
if (isCustomAPIError(err)) { console.log(err.statusCode, err.message); }
๐ฅ Why It Improves Developer Experience
๐งผ Cleaner, centralized error handling
โ Saves hours of repetitive setup
๐ง Human-readable, styled error logs
๐ซ No unnecessary dependencies (like
chalk
)โ๏ธ Easily configurable โ no lock-in, no magic
๐ Output
The build generates:
dist/index.cjs.js
โ CommonJSdist/index.esm.js
โ ESMdist/index.d.ts
โ TypeScript types
๐ผ Ideal For
Full-stack teams building REST APIs
Developers who care about clean DX
Production-ready Express.js apps
Anyone tired of writing error handlers over and over
๐งญ Want to Explore?
- ๐ View on npm
- ๐ป View source on GitHub
- ๐ Visit my portfolio
๐ License
MIT ยฉ Rashedin Islam
๐ Acknowledgements
ANSI escape codes โ for stylish logging without bloat
Built with โค๏ธ and TypeScript by Rashedin Islam
If you find it useful, consider giving it a โญ on GitHub.
Subscribe to my newsletter
Read articles from Rashedin Islam directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Rashedin Islam
Rashedin Islam
Full-Stack JavaScript Developer | React | Node.js | TypeScript | Crafting Scalable & User-Centric Web Solutions