How to fix npm ERR! Cannot find module npm ERR! Please run `npm install` first in npm
This error occurs when Node.js cannot find a required module because dependencies haven't been installed. Run npm install to download all dependencies listed in package.json.
Quick fix
Fixes MODULE_NOT_FOUND
npm install- Run npm install to install all dependencies
- Delete node_modules and package-lock.json, then reinstall
- Clear npm cache and perform full reinstall
- Verify package.json contains the required dependency
npm installnpm ERR! Cannot find modulenpm ERR! Please run `npm install` first
npm installWhat this error means
This error occurs when Node.js attempts to load a module (dependency) that doesn't exist in your project's node_modules directory. It typically appears when you try to run a script or import a package before dependencies are installed, or when the node_modules directory becomes corrupted or incomplete. The error indicates that your project has a package.json file defining required dependencies, but those dependencies haven't been installed yet or have been lost. The error can manifest when running npm scripts, importing third-party packages, or when node_modules was deleted or corrupted.
Typical symptoms
- npm commands fail with 'Cannot find module' error messages when trying to run scripts
- Application crashes on startup with MODULE_NOT_FOUND errors for specific packages
- node_modules directory is missing or mostly empty despite having a package.json file
- npm install appears to complete successfully, but dependencies are still missing
- Error message specifies a particular package name that should be installed
Common causes
- node_modules directory is completely missing and npm install has never been run
- npm install did not complete successfully due to network interruption or permission errors
- node_modules directory or specific package files are corrupted from interrupted installations
- package-lock.json is out of sync with package.json or contains stale references
- Node.js version mismatch causing cached files or compiled native modules to become incompatible
- A package is listed in package.json but the installation process failed silently
How to fix "npm ERR! Cannot find module npm ERR! Please run `npm install` first"
The most straightforward fix is to install all dependencies listed in your package.json file:
npm installThis command reads your package.json and package-lock.json files and downloads all required dependencies into the node_modules directory.
If a simple npm install doesn't resolve the issue, the node_modules directory may be corrupted:
On macOS/Linux:
rm -rf node_modules
rm -f package-lock.json
npm installOn Windows (PowerShell):
Remove-Item -Recurse -Force node_modules
Remove-Item package-lock.json
npm installnpm maintains a cache that can become corrupted. Clear it and perform a complete reinstall:
npm cache clean --force
rm -rf node_modules
rm -f package-lock.json
npm installThe --force flag bypasses safety checks in case the cache is severely corrupted.
Check that the missing module is actually listed in your package.json:
{
"dependencies": {
"express": "^4.18.0",
"lodash": "^4.17.21"
},
"devDependencies": {
"jest": "^29.0.0"
}
}If the package is missing from package.json, install it explicitly:
npm install package-name --saveFor development dependencies:
npm install package-name --save-devIn some cases, npm fails to install dependencies due to peer dependency conflicts:
npm install --legacy-peer-depsThis should only be used as a temporary workaround. Ideally, update your packages to compatible versions.
Version mismatches between Node.js and npm can cause installation issues:
node --version
npm --versionIf you've recently updated Node.js, perform a fresh reinstall:
rm -rf node_modules package-lock.json
npm cache clean --force
npm installAfter installing dependencies, restart any running development servers:
# Stop your current process (Ctrl+C)
# Then restart your server
npm startAdvanced notes
If standard troubleshooting doesn't work, use diagnostic commands: npm list shows all installed packages and warns about missing dependencies. For specific packages, npm list package-name verifies installation location. In monorepo setups using npm workspaces, ensure you run npm install from the root directory. For CI/CD pipelines, use npm ci (clean install) instead of npm install for reproducible builds with exact package-lock.json versions. On Windows with antivirus software, file access restrictions can interrupt installations—temporarily disable antivirus or add node_modules to exclusion lists.
Frequently asked questions
- What does "MODULE_NOT_FOUND" mean?
- js attempts to load a module (dependency) that doesn't exist in your project's node_modules directory. It typically appears when you try to run a script or import a package before dependencies are installed, or when the node_modules directory becomes corrupted or incomplete. json file defining required dependencies, but those dependencies haven't been installed yet or have been lost.
- How do I fix "MODULE_NOT_FOUND"?
- This error occurs when Node.js cannot find a required module because dependencies haven't been installed. Run npm install to download all dependencies listed in package.json.
- What causes "MODULE_NOT_FOUND"?
- node_modules directory is completely missing and npm install has never been run npm install did not complete successfully due to network interruption or permission errors node_modules directory or specific package files are corrupted from interrupted installations
- Is the "MODULE_NOT_FOUND" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- 📚npm Official Documentation
- 📖npm docs: npm install(Official Docs)
- 🐙GitHub: npm install issues(GitHub)
- 💬Stack Overflow: npm install first(Stack Overflow)