How to fix Error: Cannot find module 'express' in Node.js

Node.jsBEGINNERHIGHUpdated Dec 9, 2025ยท 302 views

This error occurs when Node.js cannot locate a module you are trying to import or require. It typically happens when a package is not installed, the path is incorrect, or dependencies are corrupted.

Quick fix

Fixes MODULE_NOT_FOUND

npm install express
  1. Install the missing package
  2. Install all project dependencies
  3. Fix local module import paths
  4. Perform a clean reinstall
node.js โ€” error
npm install express
Error: Cannot find module 'express'
shell
npm install express

What this error means

The "Cannot find module" error is Node.js's way of telling you it cannot locate a module you're trying to load with require() or import. When Node.js encounters a require() or import statement, it follows a specific module resolution algorithm to find the module. It first checks for core modules (like 'fs' or 'http'), then looks in node_modules directories starting from the current directory and moving up the directory tree, and finally tries to resolve relative or absolute file paths. This error most commonly occurs with third-party packages like Express, but can also happen with local modules if the path is incorrect. The error message typically includes the name of the missing module, making it relatively easy to identify what's missing. Understanding Node.js's module resolution process is key to diagnosing and fixing this error quickly.

Typical symptoms

  • Application crashes immediately on startup with MODULE_NOT_FOUND error
  • Error message shows the exact module name that cannot be found
  • Code runs fine on one machine but fails on another
  • Error occurs after cloning a repository or switching branches
  • Import or require statements fail at runtime

Common causes

  1. The required package was never installed with npm install
  2. The node_modules directory is missing or incomplete
  3. Incorrect relative or absolute path when importing local files
  4. Package was installed globally instead of locally to the project
  5. Corrupted node_modules directory or npm cache
  6. Case sensitivity mismatch in module names (especially on Linux)
  7. Missing package.json file or incorrect dependencies configuration
  8. Package is listed in devDependencies but required in production code

How to fix "Error: Cannot find module 'express'"

1Install the missing package

If the error mentions a third-party package (like 'express', 'cors', etc.), install it locally in your project:

bash
npm install express

For multiple packages, install them all at once:

bash
npm install express cors body-parser

Never use the -g (global) flag for project dependencies, as Node.js looks for modules in the local node_modules directory first.

2Install all project dependencies

If you've just cloned a repository or the node_modules directory is missing, install all dependencies listed in package.json:

bash
npm install

This command reads package.json and package-lock.json (if present) and installs all required dependencies into the node_modules directory. Make sure you run this command in the project root directory where package.json is located.

3Fix local module import paths

If the error is for a local file you created, ensure the path is correct. For CommonJS:

javascript
// Wrong - missing './'
const myModule = require('utils/helper');

// Correct - relative path
const myModule = require('./utils/helper');

For ES Modules (when using "type": "module" in package.json), include the file extension:

javascript
// Wrong - missing extension
import myModule from './utils/helper';

// Correct - includes .js extension
import myModule from './utils/helper.js';

Paths are case-sensitive on Linux servers, so ensure exact casing matches your file system.

4Perform a clean reinstall

If dependencies are corrupted, do a complete clean reinstall. On macOS/Linux:

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

On Windows (Command Prompt):

bash
rd /s /q "node_modules"
del package-lock.json
npm cache clean --force
npm install

This removes all installed packages, clears the npm cache, and reinstalls everything fresh from the registry. Restart your IDE or terminal after this process.

5Verify package.json dependencies

Open your package.json and ensure the module is listed in the correct section:

json
{
  "dependencies": {
    "express": "^4.18.2"
  },
  "devDependencies": {
    "nodemon": "^3.0.1"
  }
}

Production dependencies (needed to run the app) belong in dependencies. Development tools (testing, building) go in devDependencies. Some deployment platforms prune devDependencies, which can cause MODULE_NOT_FOUND errors in production if packages are misplaced.

Advanced notes

TypeScript-specific issues: When using TypeScript with Node.js built-in modules (like 'fs' or 'path'), you may see this error if @types/node is not installed. Fix with: npm install --save-dev @types/node

ES Modules vs CommonJS: If you have "type": "module" in package.json, you must use ESM syntax (import/export) and include file extensions for local imports. Mixing require() with ES modules causes errors.

Monorepo or workspace issues: In monorepos using npm workspaces or Yarn workspaces, modules may be hoisted to the root node_modules. If a package is not found, ensure it's declared in the correct package.json.

Docker/containerization: When containerizing Node.js apps, ensure your Dockerfile copies package.json and runs npm install before copying source code. Also verify node_modules is in .dockerignore to avoid copying local dependencies into the container.

Production deployment: Many platforms (Heroku, Vercel, Railway) run npm install --production or npm ci --production, which skips devDependencies. Ensure runtime dependencies are in the correct section.

Frequently asked questions

What does "MODULE_NOT_FOUND" mean?
js's way of telling you it cannot locate a module you're trying to load with require() or import. js encounters a require() or import statement, it follows a specific module resolution algorithm to find the module. It first checks for core modules (like 'fs' or 'http'), then looks in node_modules directories starting from the current directory and moving up the directory tree, and finally tries to resolve relative or absolute file paths.
How do I fix "MODULE_NOT_FOUND"?
This error occurs when Node.js cannot locate a module you are trying to import or require. It typically happens when a package is not installed, the path is incorrect, or dependencies are corrupted.
What causes "MODULE_NOT_FOUND"?
The required package was never installed with npm install The node_modules directory is missing or incomplete Incorrect relative or absolute path when importing local files
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