Error Fix Guide
How to Fix "Cannot Find Module" in Node.js
The most common Node.js error — and the most fixable. This guide walks through every cause with the exact fix for each.
Quick answer: Run npm install in your project folder. If that fails, delete node_modules and package-lock.json, then reinstall. If it still fails, the issue is likely a wrong import path or an ESM/CommonJS mismatch.
🔍 What the Error Actually Means
Error: Cannot find module 'express'
Require stack:
- /Users/you/project/index.js
Node.js is saying: "I looked for a module called 'express' and couldn't find it." This happens for four reasons:
- The module isn't installed
- The import path is wrong
- The module name is misspelled
- You're using the wrong module system (CommonJS vs ES Modules)
Fix 1 — Install the Module
The most common cause. If the error says Cannot find module 'express' and you haven't installed Express, install it:
npm install express
Or install all dependencies listed in package.json:
npm install
Verify the module is in your package.json and node_modules:
ls node_modules | grep express
Fix 2 — Clean Reinstall
If the module is installed but Node still can't find it, your node_modules is likely corrupted. Wipe and reinstall:
rm -rf node_modules package-lock.json
npm cache clean --force
npm install
On Windows PowerShell:
Remove-Item -Recurse -Force node_modules, package-lock.json
npm cache clean --force
npm install
💡 This fixes ~70% of "Cannot find module" errors. If it didn't, move to Fix 3.
Fix 3 — Wrong Import Path
For local files (not npm packages), the path must be correct and include the extension in some cases:
// ❌ Wrong — missing ./
const utils = require('utils');
// ✅ Right
const utils = require('./utils');
// ❌ Wrong — ESM requires the extension
import { helper } from './utils';
// ✅ Right
import { helper } from './utils.js';
Case sensitivity matters too — ./Utils.js is different from ./utils.js on Linux and macOS (case-sensitive filesystem).
Fix 4 — ESM vs CommonJS Mismatch
Node has two module systems. Mixing them causes confusion:
// CommonJS (default in .js files)
const express = require('express');
module.exports = { foo };
// ES Modules (requires "type": "module" in package.json)
import express from 'express';
export { foo };
Check your package.json:
{
"type": "module" // or "commonjs"
}
Rule: If you set "type": "module", use import everywhere. If you leave it as default, use require everywhere. Don't mix.
Fix 5 — Global vs Local Packages
If you installed a package globally (npm install -g typescript), you can't import it in a project. Install it locally:
npm install typescript --save-dev
Global packages are for CLI tools you run from the terminal. Local packages are for code you import in your project.
Fix 6 — File Doesn't Exist
Check that the file actually exists at the path you're importing from:
# List files in the current folder
ls -la
# Find the file anywhere in the project
find . -name "utils.js" -not -path "*/node_modules/*"
Common mistake: importing from './helpers' when the folder is called helper (singular).
🧠 Quick Diagnosis Flowchart
Cannot find module 'X'
│
├── Is X an npm package?
│ ├── Yes → Run `npm install X`
│ │ Still failing? → Delete node_modules, reinstall
│ │
│ └── No (local file) → Check the path
│ ├── Missing ./ ? Add it
│ ├── Missing .js extension in ESM? Add it
│ └── Wrong case? Fix it
│
└── Still failing? → Check package.json type
├── "module" → use import
└── default → use require
❓ Frequently Asked Questions
How do I fix Cannot find module in Node.js?
;First run npm install. If that doesn't work, delete node_modules and package-lock.json and reinstall. Then check import paths and ESM/CommonJS settings.
Why does it happen after npm install?
The module might be listed in package.json but not actually installed. Or your import path is wrong. Or you're using ESM without the extension.
What's the difference between require and import?
require is CommonJS, the default in Node. import is ES Modules, needs "type": "module" in package.json or a .mjs extension.
How do I clear node_modules?
Run rm -rf node_modules package-lock.json (Mac/Linux) or Remove-Item -Recurse -Force node_modules (Windows), then npm install.