All posts
javascriptmodulesdebugging

Module not found — What It Means and How to Fix It

"Module not found" explained — why it happens, a real code example that triggers it, and the exact fix.

SR

Suhail Roushan

August 6, 2026

·
3 min read
·
0 views

"Module not found" is a resolution failure where your runtime or compiler cannot locate the file or package you asked to import.

What "Module not found" Means

When JavaScript, TypeScript, or a bundler processes an import or require statement, it resolves that path against your file system and node_modules. If the file doesn't exist, the path is wrong, or the package isn't installed, you get Module not found: Can't resolve '...' (Webpack) or Cannot find module '...' (Node.js). The error always names the exact module and usually the importing file, so the first step is reading the full message — not just the headline.

Why It Happens

The three most common causes, in order of frequency:

  1. Typo or case mismatch./components/Button vs ./components/button. On Linux/macOS, file systems are case-sensitive; Node.js and bundlers follow that.
  2. Missing dependency — you wrote import axios from 'axios' but never ran npm install axios. The package isn't in node_modules.
  3. Wrong relative path depth../ instead of ./, or one level too shallow/deep. This is rampant in deeply nested component folders.

Example Code That Triggers It

Create this file in a fresh Node.js project (Node 20+):

// src/utils/format.ts
export function formatPrice(n: number): string {
  return `$${n.toFixed(2)}`;
}
// src/index.ts
import { formatPrice } from './util/format'; // typo: 'util' instead of 'utils'

console.log(formatPrice(19.99));

Run with npx tsx src/index.ts. You'll get:

Error: Cannot find module './util/format'

The path resolves to src/util/format.ts, which doesn't exist — the real folder is src/utils.

How to Fix It

Correct the path:

// src/index.ts
import { formatPrice } from './utils/format'; // fixed

console.log(formatPrice(19.99));

Now it resolves. The fix works because the relative path now points to the actual file. For package imports, the fix is npm install <package-name> — that puts the module into node_modules, where the resolver looks next.

Common Mistakes That Cause This

Mistake 1: Relying on editor autocomplete without verifying the file exists. VS Code will happily suggest a path from a stale index or a file you deleted. Always check the actual file tree with ls or the Explorer panel before importing.

Mistake 2: Adding imports before installing dependencies. Developers often write the import line first, then run npm install after — but if the install fails silently or you're in a monorepo with hoisted deps, the package lands in the wrong node_modules level. Run npm ls <package> to verify it's where your app expects.

When Should You Worry About This?

If you get Module not found on a fresh clone of a working repository, it's almost always a missing install — run npm install first. If that fails, check your lockfile (package-lock.json or yarn.lock) for version mismatches. If the error persists on a path that visibly exists, you're dealing with a bundler alias issue (like @/ pointing to the wrong root) — check tsconfig.json paths or your bundler's resolve.alias config. That's the rare case worth debugging beyond the obvious.

Next time you see this error, check the file path character-for-character against your actual directory structure — before touching npm install or clearing caches. Nine times out of ten, it's a typo.

Related posts

Written by Suhail Roushan — Full-stack developer. More posts on AI, Next.js, and building products at suhailroushan.com/blog.

Get in touch