Node.js4 min read2026-03-01
Node.js Cannot Find Module (Error: MODULE_NOT_FOUND)
Troubleshoot Error: Cannot find module '...' in Node.js applications and ES modules.
Error Code / Stack Trace
Error: Cannot find module 'express'
Require stack:
- /app/server.jsProblem Overview
Node.js runtime cannot find the requested package in node_modules or cannot resolve the specified file path.
Why Does This Happen?
- Forgot to run npm install after cloning or switching git branches.
- CommonJS vs ES Module extension mismatch (missing .js extension in ESM import).
- File name casing mismatch between local dev and production server.
Step-by-Step Solution
Step 1: Run clean npm install
Ensure all dependencies in package.json are downloaded.
bash
npm installStep 2: Add .js extension for native ES Modules
If your package.json has 'type': 'module', native Node.js requires explicit file extensions.
javascript
// BAD in Node ESM:
import { helper } from './utils';
// GOOD:
import { helper } from './utils.js';Common Mistakes to Avoid
- •Assuming node_modules resolves globally when dependencies were installed locally.
Prevention & Best Practices
- Always commit package-lock.json to ensure deterministic installs.
Frequently Asked Questions
Why does Node.js ESM require file extensions while Webpack doesn't?
Node.js follows the Web/URL spec for ESM resolution to avoid costly filesystem probing on every import.
Related Developer Solutions & Tools
Recommended Tools
Related Error Fixes