How to fix Error: Cannot find module 'express' in Node.js
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- Install the missing package
- Install all project dependencies
- Fix local module import paths
- Perform a clean reinstall
npm install expressError: Cannot find module 'express'
npm install expressWhat 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
- 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
- Package was installed globally instead of locally to the project
- Corrupted node_modules directory or npm cache
- Case sensitivity mismatch in module names (especially on Linux)
- Missing package.json file or incorrect dependencies configuration
- Package is listed in devDependencies but required in production code
How to fix "Error: Cannot find module 'express'"
If the error mentions a third-party package (like 'express', 'cors', etc.), install it locally in your project:
npm install expressFor multiple packages, install them all at once:
npm install express cors body-parserNever use the -g (global) flag for project dependencies, as Node.js looks for modules in the local node_modules directory first.
If you've just cloned a repository or the node_modules directory is missing, install all dependencies listed in package.json:
npm installThis 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.
If the error is for a local file you created, ensure the path is correct. For CommonJS:
// 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:
// 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.
If dependencies are corrupted, do a complete clean reinstall. On macOS/Linux:
rm -rf node_modules
rm -f package-lock.json
npm cache clean --force
npm installOn Windows (Command Prompt):
rd /s /q "node_modules"
del package-lock.json
npm cache clean --force
npm installThis removes all installed packages, clears the npm cache, and reinstalls everything fresh from the registry. Restart your IDE or terminal after this process.
Open your package.json and ensure the module is listed in the correct section:
{
"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
- ๐Node.js Official Documentation
- ๐Node.js Modules Documentation(Official Docs)
- ๐npm install Documentation(Official Docs)
- ๐Node.js ECMAScript Modules(Official Docs)
- ๐