npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

why-error

v1.0.0

Published

Understand JavaScript errors in simple English.

Readme

why-error

Don't just see the error. Understand it.

License: MIT Node.js


What is why-error?

why-error is a lightweight, zero-dependency JavaScript package that explains common JavaScript error messages in plain, beginner-friendly English.

Instead of just showing you a cryptic error like this:

TypeError: Cannot read properties of undefined (reading 'name')

why-error tells you what went wrong, why it happened, and exactly how to fix it — with a small code example included.


The Problem It Solves

When you are learning JavaScript, error messages feel like a foreign language. You paste the error into Google, wade through five Stack Overflow tabs, and still aren't sure what to do.

why-error fixes that. It is designed to be the friendly senior developer sitting next to you — explaining the error in simple words, right there in your terminal or in your code.


Features

| Feature | Details | |---|---| | 📖 Plain-English explanations | Every explanation is written for beginners | | 🔍 Why it happened | Not just what, but why | | 🛠️ How to fix it | Actionable steps, not vague advice | | 💡 Code examples | A short snippet showing the fix | | 🖥️ CLI tool | Use it directly from your terminal | | 🎨 Coloured output | Easier to read at a glance | | ⚫ --no-color flag | Works great in scripts and CI environments | | 🛡️ Safe input handling | Never crashes on null, undefined, or empty input | | 📦 Zero dependencies | Pure Node.js — nothing to install | | ✅ Tested | 31 automated tests covering library and CLI |


Installation

npm install why-error

To use it locally right now, clone the repository and use it from the project folder:

git clone https://github.com/your-username/why-error.git
cd why-error

Library Usage

Use why-error inside your own JavaScript files by importing explainError().

Basic example

const { explainError } = require('why-error');

const err = new TypeError("Cannot read properties of undefined (reading 'name')");
const result = explainError(err);

console.log(result);

Output

{
  type:        'TypeError',
  title:       'Accessing a property on undefined',
  explanation: "You are trying to read a property from a variable that is 'undefined'. " +
               "'undefined' means the variable was never given a value.",
  reason:      'The variable you expected to hold an object is empty (undefined) at the time you accessed it.',
  solution:    'Make sure the variable has a value before you try to use its properties. ' +
               'You can use optional chaining (?.) to safely access nested properties.',
  example:     '// Safe access with optional chaining\nconst name = user?.name;',
  recognized:  true
}

Passing a plain string

You can also pass a string — useful when you have copied an error from your console:

const { explainError } = require('why-error');

const result = explainError("ReferenceError: myVar is not defined");

console.log(result.explanation);
// → "You are trying to use a variable or function that JavaScript has never heard of."

Return value

Every call to explainError() returns an object with these seven fields:

| Field | Type | Description | |---|---|---| | type | string | The error type, e.g. "TypeError" | | title | string | A short label for the specific error pattern | | explanation | string | What the error means in plain English | | reason | string | Why it most likely happened | | solution | string | How to fix it | | example | string | A small code snippet demonstrating the fix | | recognized | boolean | true if the error is in the database, false for the fallback |


CLI Usage

why-error ships with a command-line tool so you can explain errors directly from your terminal — no code required.

Local usage (before publishing)

node bin/why-error.js "your error message here"

After publishing to npm (global install)

npm install -g why-error
why-error "your error message here"

Examples

# ReferenceError
node bin/why-error.js "x is not defined"

# TypeError — undefined property access
node bin/why-error.js "Cannot read properties of undefined (reading 'name')"

# TypeError — not a function
node bin/why-error.js "myFunction is not a function"

# SyntaxError (prefix is optional)
node bin/why-error.js "SyntaxError: Unexpected token }"

# RangeError
node bin/why-error.js "Maximum call stack size exceeded"

# Const reassignment
node bin/why-error.js "TypeError: Assignment to constant variable."

# Multi-word input — quotes are optional in most shells
node bin/why-error.js x is not defined

Sample terminal output

========================================
               WHY-ERROR
========================================

ERROR TYPE:
ReferenceError

WHAT HAPPENED?
You are trying to use a variable or function that JavaScript
has never heard of. It has not been declared anywhere in
your code.

WHY DID IT HAPPEN?
You may have made a typo, forgotten to declare the variable
with 'let', 'const', or 'var', or you are using it outside
the scope where it was declared.

HOW TO FIX IT?
Make sure the variable is declared before you use it, and
that you are using it in the right place (scope) in your
code.

EXAMPLE:
// Declare the variable before using it
let userName = 'Alice';
console.log(userName); // ✓

========================================

Available CLI options

| Option | Description | |---|---| | --help, -h | Show the usage guide | | --version, -v | Print the version number | | --no-color | Disable coloured output |

Disabling colors

Pass --no-color:

node bin/why-error.js --no-color "x is not defined"

Or set the NO_COLOR environment variable (works with any tool that respects the standard):

NO_COLOR=1 node bin/why-error.js "x is not defined"

Supported Errors

| Error | Pattern matched | |---|---| | TypeError | Cannot read properties of undefined | | TypeError | Cannot read properties of null | | TypeError | is not a function | | TypeError | Assignment to constant variable | | ReferenceError | is not defined | | SyntaxError | Unexpected token | | SyntaxError | Unexpected end of input | | RangeError | Maximum call stack size exceeded | | RangeError | Invalid array length |

For errors that are not in the list, why-error returns a friendly fallback explanation and sets recognized: false. It never crashes.


Running the Tests

This project uses Node.js's built-in test runner — no extra packages needed.

npm test

Expected output:

✔ TypeError — accessing property of undefined (Error object)
✔ TypeError — accessing property of null (Error object)
...
ℹ tests 31
ℹ pass  31
ℹ fail  0

Project Structure

why-error/
├── bin/
│   └── why-error.js   ← CLI entry point (Phase 2)
├── src/
│   ├── index.js       ← explainError() function (Phase 1)
│   └── errors.js      ← Error definitions and explanations (Phase 1)
├── test/
│   └── test.js        ← 31 automated tests (library + CLI)
├── package.json
├── README.md
├── LICENSE
└── .gitignore

Limitations

  • Coverage: Only 9 specific error patterns are recognised in this version. Errors that don't match any pattern fall back to a generic message.
  • No runtime execution: why-error does not run your code or access the internet. It matches patterns from a local list.
  • English only: Explanations are currently available in English only.
  • Node.js only: The package requires Node.js ≥ 18 and does not run in a browser.

Roadmap

| Phase | Feature | |---|---| | Phase 1 ✅ | Core library — explainError() | | Phase 2 ✅ | CLI tool with coloured output | | Phase 3 ✅ | Testing, documentation, npm preparation | | Phase 4 🔜 | Stdin support (cat error.log \| why-error) | | Future | More error patterns (EvalError, URIError, promise rejections) | | Future | Direct links to MDN documentation | | Future | Multi-language support | | Future | VS Code extension |


Contributing

This package is a learning project. Contributions, suggestions, and feedback are welcome!

If you find an error message that why-error does not recognise, feel free to open an issue or pull request.


License

MIT © 2026 why-error contributors