Universal TypeScript and JavaScript logger with pretty terminal output, structured fields-first JSON, secret masking and zero runtime dependencies.
ehmicky/modern-errors
A JavaScript/TypeScript error-handling library for creating error classes, wrapping and aggregating errors, and normalizing invalid exceptions.
- Stars
- 1,487
- Forks
- 20
- Open issues
- 1
- Last commit
- 9 days ago
What is it?
modern-errors is a JavaScript library for handling errors in a consistent way across Node.js and browsers. You define error classes with a simple subclass API, attach custom properties to errors, and wrap an inner error with an outer one using the standard cause option while keeping the inner error's message, stack and class information.
The library also normalizes invalid thrown values, such as strings or objects that are not Error instances, into proper error instances. Calling BaseError.normalize() at a module's entry point ensures every thrown error is valid and falls into either a known class or a default UnknownError class, which helps separate expected failures from bugs.
Functionality can be extended through plugins for serialization, CLI handling, pretty-printed stacks, HTTP error responses and Winston logging. The package is an ES module with strict TypeScript types and requires Node.js 18.18.0 or later.
Key features
- 01Create error classes with a simple subclass API.
- 02Attach custom properties to errors, including non-enumerable internal ones.
- 03Wrap inner errors with the standard cause option, merging their messages and stacks.
- 04Aggregate multiple errors into one, even for custom error classes.
- 05Normalize invalid thrown values into proper error instances.
- 06Extend behavior through plugins and strict TypeScript types.
Use cases
- Defining a shared set of error classes for a Node.js or browser library.
- Separating expected user input errors from unexpected bugs at a module boundary.
- Adding context to errors as they propagate through layers of code.
- Serializing errors to JSON and restoring them with the serialize plugin.
Quick start
- 1
Install the library with npm
npm install modern-errors - 2
Create error classes with ModernError.subclass and import them where needed
Things to know
- Requires Node.js 18.18.0 or newer.
- Is an ES module only and cannot be loaded with require().
Topics
FAQ
- What is modern-errors?
- A JavaScript/TypeScript error-handling library for creating error classes, wrapping and aggregating errors, and normalizing invalid exceptions. modern-errors is a JavaScript library for handling errors in a consistent way across Node.js and browsers. You define error classes with a simple subclass API, attach custom properties to errors, and wrap an inner error with an outer one using the standard cause option while keeping the inner error's message, stack and class information.
- Is modern-errors free to use? What license does it have?
- modern-errors is open source under the MIT license. Check the license terms before using it in commercial products.
- Is modern-errors still maintained?
- Actively maintained. The last commit to modern-errors was on Oct 13, 2026.
- What can I use modern-errors for?
- Defining a shared set of error classes for a Node.js or browser library. Separating expected user input errors from unexpected bugs at a module boundary. Adding context to errors as they propagate through layers of code. Serializing errors to JSON and restoring them with the serialize plugin.
- How do I install modern-errors?
- Install the library with npm: npm install modern-errors Create error classes with ModernError.subclass and import them where needed
- Where can I get modern-errors?
- The source code and installation instructions are on GitHub: https://github.com/ehmicky/modern-errors
Summary generated by AI from the project's README. GitHub stats refresh daily.
Related projects
A TypeScript re-implementation of the ADB client that runs in Chromium browsers, Node.js and Electron, connecting to Android devices over WebUSB.
A JavaScript library that extracts dominant colors and color palettes from images in both the browser and Node.js.
A VSCode extension that reformats TypeScript error messages into readable, syntax-highlighted types with links to docs and explanations.
A CLI that scores the code quality of a project across 14 languages and produces a report with an overall score and per-file rankings.
Open-source API and integration testing tool that records real traffic via eBPF and replays it as tests and data mocks, with no code changes.