How to fix npm ERR! Cannot find module npm ERR! Please run `npm install` first in npm

npmBEGINNERHIGHUpdated Dec 5, 2025· 539 views

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
  1. Run npm install to install all dependencies
  2. Delete node_modules and package-lock.json, then reinstall
  3. Clear npm cache and perform full reinstall
  4. Verify package.json contains the required dependency
npm — error
npm install
npm ERR! Cannot find module
npm ERR! Please run `npm install` first
shell
npm install

What 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

  1. node_modules directory is completely missing and npm install has never been run
  2. npm install did not complete successfully due to network interruption or permission errors
  3. node_modules directory or specific package files are corrupted from interrupted installations
  4. package-lock.json is out of sync with package.json or contains stale references
  5. Node.js version mismatch causing cached files or compiled native modules to become incompatible
  6. 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"

1Run npm install to install all dependencies

The most straightforward fix is to install all dependencies listed in your package.json file:

bash
npm install

This command reads your package.json and package-lock.json files and downloads all required dependencies into the node_modules directory.

2Delete node_modules and package-lock.json, then reinstall

If a simple npm install doesn't resolve the issue, the node_modules directory may be corrupted:

On macOS/Linux:

bash
rm -rf node_modules
rm -f package-lock.json
npm install

On Windows (PowerShell):

powershell
Remove-Item -Recurse -Force node_modules
Remove-Item package-lock.json
npm install
3Clear npm cache and perform full reinstall

npm maintains a cache that can become corrupted. Clear it and perform a complete reinstall:

bash
npm cache clean --force
rm -rf node_modules
rm -f package-lock.json
npm install

The --force flag bypasses safety checks in case the cache is severely corrupted.

4Verify package.json contains the required dependency

Check that the missing module is actually listed in your package.json:

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:

bash
npm install package-name --save

For development dependencies:

bash
npm install package-name --save-dev
5Resolve peer dependency conflicts with --legacy-peer-deps

In some cases, npm fails to install dependencies due to peer dependency conflicts:

bash
npm install --legacy-peer-deps

This should only be used as a temporary workaround. Ideally, update your packages to compatible versions.

6Check Node.js version and restart your development server

Version mismatches between Node.js and npm can cause installation issues:

bash
node --version
npm --version

If you've recently updated Node.js, perform a fresh reinstall:

bash
rm -rf node_modules package-lock.json
npm cache clean --force
npm install

After installing dependencies, restart any running development servers:

bash
# Stop your current process (Ctrl+C)
# Then restart your server
npm start

Advanced 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