How to fix Error: Cannot find module 'lodash' Require stack: - /app/index.js in Node.js

Node.jsBEGINNERHIGHUpdated Dec 9, 2025ยท 210 views

Node.js throws a ModuleNotFoundError when it cannot locate a required module in the node_modules directory or module resolution paths. This typically happens when a package is not installed or the import path is incorrect.

Quick fix

Fixes MODULE_NOT_FOUND

npm install lodash
  1. Install the missing package
  2. Install all project dependencies
  3. Verify the module name spelling
  4. Check package.json dependencies
node.js โ€” error
npm install lodash
Error: Cannot find module 'lodash'
Require stack:
- /app/index.js
shell
npm install lodash

What this error means

The "Cannot find module" error occurs when Node.js's module resolution system fails to locate a package you're trying to import or require. Node.js searches for modules in several locations: the local node_modules folder, parent directories up the file tree, and global installation paths. When the specified module isn't found in any of these locations, Node.js throws a ModuleNotFoundError. This error is one of the most common issues developers encounter when working with Node.js applications, especially when setting up new projects, cloning repositories, or managing dependencies. The module system expects packages to be properly installed and available in the node_modules directory before they can be imported. Understanding Node.js module resolution is key to fixing this error. Node.js follows a specific algorithm: it starts looking for the module in the current directory's node_modules, then checks parent directories recursively until it reaches the root. For npm packages, the module must be listed in package.json dependencies and physically present in node_modules after running npm install.

Typical symptoms

  • Application crashes immediately on startup with MODULE_NOT_FOUND error
  • Error stack trace shows the exact file where the import/require statement fails
  • Build process fails during compilation or bundling phase
  • Tests fail with module resolution errors
  • Fresh git clone fails to run without npm install
  • Different behavior between development and production environments
  • IDE shows red underlines on import statements

Common causes

  1. Package not installed - missing from node_modules directory
  2. Dependencies not installed after cloning repository (npm install not run)
  3. Typo in the module name or incorrect capitalization
  4. Package listed in devDependencies but used in production code
  5. node_modules deleted or corrupted
  6. Package version incompatibility or dependency conflict
  7. Incorrect import path for local modules (missing ./ or ../)
  8. Case sensitivity issues when moving between operating systems
  9. npm cache corruption preventing proper installation
  10. Missing package.json file in the project root

How to fix "Error: Cannot find module 'lodash' Require stack: - /app/index.js"

1Install the missing package

Run npm install with the package name to add it to your project:

bash
npm install lodash

For packages only needed in development (like testing frameworks), use:

bash
npm install --save-dev lodash

This adds the package to node_modules and updates package.json automatically.

2Install all project dependencies

If you've just cloned a repository or package.json exists but node_modules is missing, install all dependencies:

bash
npm install

This reads package.json and installs all listed dependencies. For projects using Yarn or pnpm:

bash
# Yarn
yarn install

# pnpm
pnpm install
3Verify the module name spelling

Check your import or require statement for typos:

javascript
// Incorrect
const _ = require('lodash'); // but you typed 'lodas'

// Correct
const _ = require('lodash');

Module names are case-sensitive. Verify the exact name by checking npmjs.com or the package documentation.

4Check package.json dependencies

Open package.json and verify the module is listed:

json
{
  "dependencies": {
    "lodash": "^4.17.21"
  }
}

If it's missing, either add it manually and run npm install, or use npm install lodash to add it automatically. Ensure production dependencies aren't listed under devDependencies.

5Clear npm cache and reinstall

If installation seems successful but the error persists, clear the npm cache:

bash
# Clear npm cache
npm cache clean --force

# Delete node_modules and lockfile
rm -rf node_modules package-lock.json

# Reinstall everything
npm install

On Windows, use:

powershell
# Delete folders
Remove-Item -Recurse -Force node_modules, package-lock.json

# Reinstall
npm install
6Fix local module paths

For custom local modules, use relative paths with ./ or ../:

javascript
// Incorrect - Node looks in node_modules
const utils = require('utils');

// Correct - Explicitly references local file
const utils = require('./utils');
const config = require('../config');

Local modules without ./ or ../ are treated as npm packages.

7Verify Node.js and npm versions

Check that your Node.js and npm versions meet the project requirements:

bash
node --version
npm --version

Some packages require specific Node.js versions. Check package.json for an engines field:

json
{
  "engines": {
    "node": ">=18.0.0"
  }
}

Use nvm (Node Version Manager) to switch versions if needed:

bash
nvm install 18
nvm use 18

Advanced notes

TypeScript Projects: If using TypeScript, you may also need to install type definitions for packages that don't include them:

bash
npm install --save-dev @types/lodash

Type definition packages follow the @types/ namespace convention. Not all packages require thisโ€”many modern packages include TypeScript definitions automatically.

Module Resolution Debugging: Use Node.js's --trace-warnings flag to see detailed module resolution attempts:

bash
node --trace-warnings index.js

You can also check which version of a package is installed using:

bash
npm list lodash

Monorepo Considerations: In monorepo setups using workspaces (npm workspaces, Yarn workspaces, or pnpm workspaces), modules are hoisted to the root node_modules. Ensure you're running install commands from the correct directory and that workspace dependencies are properly linked.

ESM vs CommonJS: ES modules use import syntax while CommonJS uses require(). If you see "Cannot use import outside a module", add "type": "module" to package.json or use .mjs file extensions. Mixing module systems can cause resolution issues.

Production Environment Issues: If the error only occurs in production, verify that:
- package.json includes all dependencies (not just devDependencies)
- CI/CD pipeline runs npm ci (which installs from package-lock.json) instead of npm install
- NODE_ENV is set correctly and doesn't skip dependency installation
- Docker containers include the COPY package*.json and RUN npm ci steps

Debugging with npm ls: Check the entire dependency tree for missing or conflicting packages:

bash
# List all dependencies
npm ls

# Check specific package
npm ls lodash

# Show only problems
npm ls --depth=0

Frequently asked questions

What does "MODULE_NOT_FOUND" mean?
js's module resolution system fails to locate a package you're trying to import or require. js searches for modules in several locations: the local node_modules folder, parent directories up the file tree, and global installation paths. js throws a ModuleNotFoundError.
How do I fix "MODULE_NOT_FOUND"?
Node.js throws a ModuleNotFoundError when it cannot locate a required module in the node_modules directory or module resolution paths. This typically happens when a package is not installed or the import path is incorrect.
What causes "MODULE_NOT_FOUND"?
Package not installed - missing from node_modules directory Dependencies not installed after cloning repository (npm install not run) Typo in the module name or incorrect capitalization
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