Skip to content

How to Resolve "Error: Cannot find module 'lodash'" Error in JavaScript

Fix the "Cannot find module lodash" error in Node.js by installing the package, adding TypeScript type definitions, or performing a clean dependency reinstall.

The Error: Cannot find module 'lodash' is a very common error in the Node.js ecosystem. It is a clear and direct message: your application is trying to import or require the lodash package, but Node.js cannot find it within your project's dependencies. This almost always means the package has not been installed.

This guide will explain the root cause of this error and provide the definitive solutions for both JavaScript and TypeScript projects. You will learn how to correctly install the package, its type definitions, and the steps to perform a clean reinstall to fix any stubborn dependency issues.

Understanding the Root Cause: Node.js Module Resolution

When you write import _ from 'lodash'; in your code, the Node.js runtime performs a module resolution process. It looks for a folder named lodash inside your project's node_modules directory. If it doesn't find that folder, it has no way to load the package's code, and it throws the Cannot find module error.

The error is a direct signal that the lodash package is missing from your node_modules folder.

Solution 1 (Most Common): Install the lodash Package

The most direct solution is to install the lodash package using your package manager (NPM or Yarn).

For example, this code tries to import a package that is not installed.

// ⛔️ This line will throw the error if lodash is not installed
import _ from 'lodash';

The solution is to open your terminal in the root directory of your project (the same folder that contains your package.json file) and run the installation command.

For NPM:

npm install lodash

For Yarn:

yarn add lodash

Solution 2 (for TypeScript): Install the Type Definitions

If you are using TypeScript, you may encounter a related error even after installing lodash: Cannot find module 'lodash' or its corresponding type declarations.

This means TypeScript knows the JavaScript code is there, but it can't find the type definitions (.d.ts files) that describe the shape of the Lodash library (its functions, arguments, and return types).

Most popular JavaScript libraries have a separate, community-managed package of type definitions. For lodash, you need to install @types/lodash as a development dependency.

npm install --save-dev @types/lodash

yarn add --dev @types/lodash

Solution 3 (for Stubborn Issues): A Clean Reinstall of Dependencies

Sometimes, your node_modules folder or your lock file can get into a corrupted or inconsistent state. If the simple install command doesn't fix the issue, a full, clean reinstall is the next best step.

The Solution is to run the following commands from your project's root directory.

For macOS / Linux:

rm -rf node_modules
rm -f package-lock.json yarn.lock

npm cache clean --force

npm install

For Windows (CMD):

rd /s /q "node_modules"
del package-lock.json
del yarn.lock

npm cache clean --force

npm install

After reinstalling, restart your code editor and development server.

How to Verify the Installation

After running the installation, you can confirm everything is correct in two ways:

  1. Check package.json: Your package.json file should now contain entries for lodash (and @types/lodash for TypeScript).
    {
      "dependencies": {
        "lodash": "^4.17.21"
      },
      "devDependencies": {
        "@types/lodash": "^4.14.191"
      }
    }
  2. Check node_modules: Look inside your node_modules folder. You should see a directory named lodash (and @types/lodash if applicable).

Conclusion

The Cannot find module 'lodash' error is almost always a straightforward dependency issue.

To fix it:

  • The primary solution is to install the package by running npm install lodash or yarn add lodash.
  • If you are using TypeScript, you must also install the type definitions as a development dependency: npm install --save-dev @types/lodash.
  • If the error persists, a clean reinstall of all your project's dependencies will usually resolve any corruption issues.
Navigation