Skip to content

Error

In JavaScript, Error is the base error type that every native error type and most custom errors extend. Code throws one to report a failure, and it carries a message and a stack trace. A checkout function might reject an empty cart like this:

Language: JavaScript
> throw new Error("Cart is empty");
Uncaught Error: Cart is empty

When JavaScript Throws Error

The language reports its own failures with the more specific types, such as TypeError for calling a value that isn’t a function. Application code throws a plain Error with a throw statement to signal a failure the language can’t detect, such as invalid input. Hosts throw one too. Node.js uses it for failures outside the language, such as a missing module or file.

Every native type inherits from Error, so error instanceof Error is true for a TypeError or a RangeError too.

Syntax

The constructor takes an optional message and an optional options object:

Language: JavaScript Syntax
new Error([<message>[, <options>]])
Parameter Description
message Optional. A description of the failure, converted to a string
options Optional. An object whose cause property becomes the error’s .cause

Error works with or without new and returns a new error object either way.

Key Properties and Methods

Every native type and custom subclass inherits these members:

Member Kind Description
.message Instance property Holds the description passed to the constructor
.name Instance property Holds the type name, "Error" by default
.cause Instance property Holds the underlying error passed in options
.stack Instance property Holds a stack trace string. Non-standard, but in every major engine
.toString() Instance method Returns the name and message joined by a colon, such as "Error: Cart is empty"
Error.isError() Static method Returns true for any real error object, including one from another realm
Error.captureStackTrace() Static method Adds a .stack property to any object. Not yet standard, but in every major engine

The .cause property was added in ES2022 and is Baseline Widely available.

Custom Error Classes

A subclass of Error gives a failure its own name, so callers can tell it apart from other errors. Say a function loads a settings file and wraps any parse failure in a ConfigError that keeps the original error as its cause:

Language: JavaScript
> class ConfigError extends Error {
…   constructor(message, options) {
…     super(message, options);
…     this.name = "ConfigError";
…   }
… }
undefined

> function loadSettings(text) {
…   try {
…     return JSON.parse(text);
…   } catch (error) {
…     throw new ConfigError("Invalid settings file", { cause: error });
…   }
… }
undefined

> try {
…   loadSettings("{");
… } catch (error) {
…   [error.name, error.cause.name, error instanceof ConfigError];
… }
[ 'ConfigError', 'SyntaxError', true ]

Setting .name makes stack traces show ConfigError instead of Error. Passing options on to super() sets .cause, so the original SyntaxError stays available.

Catching Error

A catch block receives whatever was thrown, and code can throw any value, including a string. Checking for an Error object before reading .message handles the error and rethrows anything else:

Language: JavaScript
> function parsePort(text) {
…   const port = Number(text);
…   if (!Number.isInteger(port)) throw new Error(`Invalid port: ${text}`);
…   return port;
… }
undefined

> try {
…   parsePort("eighty");
… } catch (error) {
…   if (!(error instanceof Error)) throw error;
…   error.message;
… }
'Invalid port: eighty'

The instanceof check fails for errors from another realm, such as an iframe or a node:vm context, while error.name and Error.isError() still work.

Common Messages

The language itself never throws a plain Error, so these messages all come from Node.js, which uses it for module, file system, and network failures:

Message (V8) Usual Cause Fix
Cannot find module 'x' Node.js: require() names a package that isn’t installed or a path that doesn’t exist Install the package, or correct the path
Cannot find package 'x' imported from y Node.js: an import names a package that isn’t in node_modules Run npm install x in the project folder
Cannot find module 'x' imported from y Node.js: a relative import leaves out the file extension or points to the wrong file Write the full path with its extension, such as ./utils.js
ENOENT: no such file or directory, open 'x' Node.js: a file system call gets a path that doesn’t exist, often relative to the wrong folder Build the path from import.meta.dirname
listen EADDRINUSE: address already in use :::x Node.js: another process, often an earlier run of the server, already uses the port Stop the other process, or choose another port
connect ECONNREFUSED 127.0.0.1:x Node.js: nothing listens on the address the client connects to Start the server, or correct the host and port

Firefox and Safari don’t print these messages, because browsers don’t have these Node.js APIs. Browsers report similar failures as other types, such as a TypeError from fetch(). For the most common rows, the fix is to install the missing package or correct the path.

A developer holds a magnifying glass to red error parcels caught in a net inside a glass column, beside a screen showing a stack trace.

Course

JavaScript Errors and Debugging for Pythonistas

Port a food bank's donation import to JavaScript, and learn to catch, throw, and tell apart errors, check for the bad data JavaScript doesn't throw on, and find what went wrong.

basics js py

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 12, 2026