Error: Cannot find module
This error matches known, documented patterns with reliable solutions.
Quick Fix (Most Common Solution)
- Run npm install to install dependencies
- Check spelling of module name
Seeing "Error: Cannot find module"? This error can be frustrating, but it's usually fixable. It typically affects your development workflow or system. Below you'll find clear, step-by-step solutions to resolve this issue.
What This Error Means
Node.js cannot locate the module you are trying to import or require.
Frequently documented in developer and vendor support forums.
Not affiliated with browser, OS, or device manufacturers.
New here? Learn why exact error messages matter →
Common Causes
- Package not installed
- Typo in module name
- Wrong relative path
How to Fix
- Run npm install to install dependencies
- Check spelling of module name
- Verify file path is correct
Last reviewed: March 2026 How we review solutions
Why This Happens
Node.js uses a specific module resolution algorithm to locate files when you call require() or use import. It searches the local node_modules directory, walks up the directory tree checking parent node_modules folders, and finally checks the global module path. "Cannot find module" means Node exhausted all these locations without finding a match. The most straightforward cause is that the package was never installed—running your code without first running npm install leaves node_modules empty or missing entirely. Typos in the module name are equally common: require('expresss') will fail even though express is installed. Relative path errors cause a different variant of this error: require('./utils/helper') will fail if the file is actually at ./util/helper (missing the 's'). After major refactors, file moves often leave behind stale require paths that no one updated. ES module and CommonJS mismatches can also trigger this error—importing a CJS-only package with import syntax in a project configured as "type": "module" may fail if the package does not provide an ES module entry point. Native modules compiled for a different Node version or OS architecture will also fail to load with this error.
Quick Diagnostic Checklist
- Run npm install to ensure all dependencies are present
- Check the exact module name in the error for typos
- For relative imports, verify the file path exists with ls or dir
- Run node -e "console.log(require.resolve('module-name'))" to test resolution
- Check if the module needs a build step (native addons): npm rebuild
- Verify "type" field in package.json matches your import syntax (require vs import)
Debugging module resolution step by step
// Error:
// Error: Cannot find module 'dotenv'
// Require stack:
// - /home/user/project/src/config.js
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ <-- file that tried to load it
// Step 1: Check if the package is installed
// Run in terminal:
// $ ls node_modules/dotenv
// ls: cannot access 'node_modules/dotenv': No such file or directory
// <-- Not installed!
// Step 2: Install the missing package
// $ npm install dotenv
// Step 3: For relative path errors:
// Error: Cannot find module './utils/helpers'
// Check the actual file:
// $ ls src/utils/
// helper.js <-- File is "helper.js", not "helpers.js"
// Fix: Update the require path
const helper = require('./utils/helper'); // removed trailing 's'
// Step 4: For native module issues after Node upgrade:
// $ npm rebuild # recompiles native addons for current Node version
Edge Cases & Unusual Scenarios
Yarn PnP (Plug'n'Play) environment
Yarn PnP does not use a node_modules directory. Packages are stored in a zip cache, and a .pnp.cjs file handles resolution. If you see "Cannot find module" in a Yarn PnP project, run yarn install and ensure your IDE is configured for PnP.
TypeScript path aliases not resolved at runtime
TypeScript tsconfig paths (like @/utils) are only resolved at compile time. At runtime, Node still needs the real path. Use tsconfig-paths/register or a bundler to map aliases at runtime.
Symlinked package not followed
npm link creates symlinks for local development. If the linked package has its own node_modules with conflicting versions, Node may fail to resolve peer dependencies. Use npm link --install-links (npm 9+) to copy instead of symlink.
Optional follow-up
Some users ask whether saving fixes for recurring errors would be useful when the same issue appears again.
Was this explanation helpful?
Frequently Asked Questions
Why does require fail?
Node looks in node_modules and relative paths - if neither exists, it fails.
How do I debug module paths?
Use require.resolve("module") to see where Node looks.
What is the difference between "Cannot find module" and "MODULE_NOT_FOUND"?
They are the same error. Node.js sets error.code to "MODULE_NOT_FOUND" and the error message reads "Cannot find module". The code is useful for programmatic error handling in try/catch blocks.
Why does the module work in one file but not another?
Relative paths are resolved from the file that contains the require/import call. A path like "./config" in src/index.js looks for src/config.js, but the same path in src/routes/api.js looks for src/routes/config.js.
How do I debug the full module resolution path?
Set the NODE_DEBUG=module environment variable before running your script: NODE_DEBUG=module node app.js. This prints every directory Node searches, helping you identify where the lookup fails.
Related Resources
Also Known As
- Node.js error
- Node runtime error
- JavaScript server error
- Node exception
Common Search Variations
- "node js error fix"
- "node command not working"
- "node app crashing"
- "javascript server error solution"
- "node runtime crash fix"
- "how to debug node error"
Related Errors
Still Stuck?
Paste a different error message or upload a screenshot to get help instantly.