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

Rashedin IslamRashedin Islam
4 min read

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

Image description


๐Ÿ— 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

Image description

๐Ÿ“ธ 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 โ€” CommonJS

  • dist/index.esm.js โ€” ESM

  • dist/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?


๐Ÿ“ƒ License

MIT ยฉ Rashedin Islam


๐Ÿ™Œ Acknowledgements


Built with โค๏ธ and TypeScript by Rashedin Islam
If you find it useful, consider giving it a โญ on GitHub.

24
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