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.
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.
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.
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.
Error: EMFILE: too many open files, watch
EMFILE: fs.watch() limit exceeded
Error: Middleware next() called multiple times (next() invoked twice)
Express middleware next() called multiple times
Error: Worker failed to initialize (worker startup error)
Worker failed to initialize in Node.js
Error: EMFILE: too many open files, open 'file.txt'
EMFILE: too many open files
Error: cluster.fork() failed (cannot create child process)
cluster.fork() failed - Cannot create child process