Node.js / Node.js
Node.js cannot find module
Fix Node.js cannot find module errors caused by missing installs, wrong paths, or package export issues.
- Category
- Node.js
- Error signature
Error: Cannot find module- Quick fix
- Install dependencies, verify the import path, and check that the target file or package export exists.
- Updated
What this error means
Error: Cannot find module means npm or Node.js cannot resolve the project files, package metadata, dependency graph, or module path required by the command. Check the project root, lockfile, and Node version first.
Common causes
- Dependencies were not installed
- Import path is misspelled
- File extension or case does not match
- Package exports do not expose the requested path
Copy-paste commands
Check runtime versions
node --version
npm --version
Install dependencies
npm install
Clean CI-style install
npm ci
Clear npm cache
npm cache clean --force
Reset local install state
rm -rf node_modules package-lock.json
npm install
Quick fixes
- Run the command from the folder containing
package.json. - Install dependencies, verify the import path, and check that the target file or package export exists.
- Check
node --versionandnpm --version. - Use
npm cifor a clean lockfile-based install when a lockfile exists.
Step-by-step troubleshooting
- Find the first
Error: Cannot find moduleoccurrence in the npm output; later stack lines are often symptoms. - Confirm the command is running in the intended package directory.
- Compare
package.jsonandpackage-lock.jsonafter dependency changes. - Remove stale
node_modulesonly after checking whether the lockfile is committed. - Rerun the failing command with the same Node version used in CI or production.
Platform-specific fixes
CI/CD
- Prefer
npm ciin CI so the build fails whenpackage.jsonandpackage-lock.jsondrift apart.
Real-world fixes
- If the error names a peer dependency, update the plugin and framework versions together.
- If the error names a missing file, check filename casing; CI often runs on a case-sensitive filesystem.
- Install dependencies, verify the import path, and check that the target file or package export exists.
How to prevent it
- Commit lockfile changes with dependency changes.
- Pin the project Node version in
.nvmrc,.node-version, or CI configuration. - Use CI to catch dependency drift before deploy.
Related errors
- ERR_MODULE_NOT_FOUND
- npm ERR! enoent package.json
- Cannot use import statement outside a module
FAQ
What should I check first?
Start with the exact Error: Cannot find module line and the command, request, or workflow step that produced it. In npm or Node.js, the first useful clue is usually near the first failure line, not the final stack trace.
Can I ignore this error?
No. Treat it as a failed npm or Node.js step. A temporary bypass may help diagnosis, but the underlying cause should be fixed before shipping or publishing changes.
Why does this work locally but fail elsewhere?
Local machines often have cached credentials, old dependencies, different runtime versions, or network settings that CI and production do not share. Reproduce from a clean shell or clean install when possible.
How do I know the fix worked?
Rerun the smallest command, request, or deployment step that produced Error: Cannot find module. The fix is working when that step completes without the same signature and produces the expected output.