MODULE_NOT_FOUND
CommonJS require() failed to resolve a module path.
Usually happens because:
- ☑ Missing Installation
- ☑ Relative Path Typo
- ☑ Case Sensitivity
🔍 Quick Checklist:
What is MODULE_NOT_FOUND?
The MODULE_NOT_FOUND error occurs when Node.js attempts to resolve a module using the CommonJS require() function, but the module resolution algorithm fails to find the specified npm package, relative file pathway, or local file inside node_modules.
Common Causes
- Missing Installation: The requested package was never installed (forgot to run 'npm install').
- Relative Path Typo: Relative paths are typed incorrectly (e.g. require('./utils') but file is named 'util.js').
- Case Sensitivity: Linux servers are strictly case-sensitive, while Windows dev environments are not.
| Cause | Frequency |
|---|---|
| Missing Installation | ⭐⭐⭐⭐⭐ |
| Relative Path Typo | ⭐⭐⭐⭐ |
| Case Sensitivity | ⭐⭐⭐ |
Common Mistakes
- Failing to add packages to package.json dependencies, causing deployment server builds to throw MODULE_NOT_FOUND.
- Mismatching spelling cases (e.g. require('./Helper') works on Windows but throws MODULE_NOT_FOUND on Linux servers).
How to Fix
Framework-Specific Examples
Wrapping optional configurations requirements inside Express route scripts.
let config;
try {
config = require('./config.json');
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
console.warn('config.json not found, falling back to environment variables.');
config = process.env;
}
}Server Configuration Examples
Checking project build module dependencies before running production deployments.
# Handled by bundlers during compilationBest Practices
- Always use git-tracked package.json files.
- Run npm install before executing code.
Frequently Asked Questions (FAQ)
Q: What causes MODULE_NOT_FOUND?
It is thrown when Node's CJS module loader cannot find the package in `node_modules` or fails to resolve a relative path to a local file.
Q: Why does a module load fine on Windows but fail on Linux with MODULE_NOT_FOUND?
Windows filesystems are case-insensitive. Linux filesystems are strictly case-sensitive. If you write `require('./helpers')` but the file is `Helpers.js`, it will fail on Linux.
Q: How does Node resolve module lookup locations?
Node searches local `node_modules` folders, then walks up parent directories recursively until it reaches root, looking for the target package.
Q: Can I catch MODULE_NOT_FOUND in my script?
Yes. You can wrap `require('...')` statements inside `try-catch` blocks and inspect if `err.code === 'MODULE_NOT_FOUND'`.