Common Error Classes for Node.js
The `common-errors` package provides a comprehensive suite of custom error classes for Node.js, designed to offer more specific error types than native JavaScript `Error` objects. Originally intended to mirror error types found in other modern languages, it includes classes like `AlreadyInUseError`, `ArgumentError`, `NotFoundError`, and `AuthenticationRequiredError`. Key features highlighted in its documentation include the ability to append stack traces from asynchronously generated errors, dynamically generate custom error classes, and map HTTP status codes to errors for web service integration. The current stable version is 1.2.0. However, the package has not seen active development or updates in many years, with its last release dating back to 2017. Consequently, it is no longer actively maintained.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported.
cause Attempting to `require()` an ES Module or attempting to `import` a CommonJS module (like `common-errors`) directly in an ESM context without proper configuration.fixIf `common-errors` is the CJS module: use `const errors = require('common-errors');`. If your project is ESM and you need to consume this CJS module, ensure Node.js interoperability is configured, or use a dynamic `import()` if suitable. -
ReferenceError: errors is not defined
cause The main `common-errors` module, often imported as `errors`, was not correctly `require()`d or `import`ed before use.fixAdd `const errors = require('common-errors');` at the top of your file to make the error classes available. -
TypeError: Class extends value undefined is not a constructor or null
cause This can occur if `errors.generateClass` is called with an invalid base class or if the `errors` object itself is `undefined` due to an incorrect import. It can also happen when attempting to extend a non-constructor.fixVerify that `common-errors` is correctly `require()`d and that `generateClass` is used with valid arguments. Ensure the base class (second argument) if provided, is a valid constructor function. -
Error: Cannot find module 'common-errors'
cause The `common-errors` package is not installed in the project's `node_modules` directory or there's a typo in the `require()` path.fixRun `npm install common-errors` to install the package. Double-check the module name in your `require()` statement.
Warnings
- breaking This package is effectively abandoned. The last version (1.2.0) was published 9 years ago (as of April 2026). This means there will be no new features, bug fixes, or security patches.
- gotcha The package is primarily a CommonJS module and does not natively support ES Module (`import`) syntax. Attempting to `import` it directly in a modern ESM-only project will likely result in `ERR_REQUIRE_ESM` or similar errors.
- gotcha Due to its age (last updated 2017), `common-errors` may not be fully compatible with very recent Node.js versions or modern JavaScript language features (e.g., `async/await` error handling patterns).
- breaking The lack of maintenance implies potential unaddressed security vulnerabilities. Using abandoned software in production can expose applications to risks.
Install
-
npm install common-errors -
yarn add common-errors -
pnpm add common-errors
Imports
- errors
import * as errors from 'common-errors';
const errors = require('common-errors'); - ArgumentError
import { ArgumentError } from 'common-errors';const { ArgumentError } = require('common-errors'); - generateClass
import { generateClass } from 'common-errors';const { generateClass } = require('common-errors');
Quickstart
const errors = require('common-errors');
try {
// Example 1: Argument Null Error
function processUser(username) {
if (!username) {
throw new errors.ArgumentNullError('username');
}
console.log(`Processing user: ${username}`);
}
processUser(null);
} catch (error) {
console.error('Caught ArgumentNullError:', error.name, error.message);
console.error(error.stack);
}
try {
// Example 2: Custom Error Generation
const MyCustomError = errors.generateClass('MyCustomError', 'CustomErrorBase');
throw new MyCustomError('Something went wrong in my custom component!');
} catch (error) {
console.error('Caught MyCustomError:', error.name, error.message);
}
// Example 3: Error with inner error and stack prepending
function fetchData() {
try {
throw new Error('Original underlying database error');
} catch (err) {
// Prepend the current stack to the inner error's stack
const connectionError = new errors.ConnectionError('Failed to connect to DB.', errors.prependCurrentStack(err));
throw connectionError;
}
}
try {
fetchData();
} catch (error) {
console.error('Caught ConnectionError with inner error:', error.name, error.message);
console.error('Full stack:', error.stack);
}