Last modified: Jun 22, 2026

Fix Cannot Find Module Lodash Error

Running into the "Cannot find module 'lodash'" error in Node.js can be frustrating. This common issue often stops your application from starting. But don't worry. This guide will show you exactly how to fix it.

We will cover the main causes. You will get clear, step-by-step solutions. By the end, you will understand how to prevent this error in the future. This article is perfect for beginners and experienced developers alike.

What Causes This Error?

The error message Error: Cannot find module 'lodash' means Node.js cannot locate the Lodash library. Lodash is a popular JavaScript utility library. It provides helpful functions for arrays, objects, and strings.

This problem usually happens for three reasons:

  • Lodash is not installed in your project.
  • The node_modules folder is missing or corrupted.
  • The import path is incorrect in your code.

Let's look at each cause and how to fix it.

Solution 1: Install Lodash

The most common fix is to install Lodash. Open your terminal in the project root folder. Then run one of these commands:


# Using npm
npm install lodash

# Using yarn
yarn add lodash

This command adds Lodash to your node_modules folder. It also updates your package.json file. After installation, try running your application again.

Solution 2: Reinstall All Dependencies

Sometimes the node_modules folder gets corrupted. The best fix is to delete it and reinstall everything. Follow these steps:


# Delete node_modules folder
rm -rf node_modules

# Delete package-lock.json (optional but recommended)
rm package-lock.json

# Reinstall all dependencies
npm install

This process ensures you have a fresh copy of all modules. After this, your Lodash error should be gone. If you still see issues, check your import statements.

Solution 3: Check Your Import Statement

A wrong import path can also cause this error. Make sure you are importing Lodash correctly in your code. Here is the proper way:


// Correct import for the entire library
const _ = require('lodash');

// Example usage
const result = _.chunk(['a', 'b', 'c', 'd'], 2);
console.log(result);
// Output: [ [ 'a', 'b' ], [ 'c', 'd' ] ]

If you are using ES modules, use this syntax:


// For ES modules
import _ from 'lodash';

// Example usage
const numbers = [1, 2, 3, 4, 5];
const shuffled = _.shuffle(numbers);
console.log(shuffled);
// Output: [ 3, 1, 5, 2, 4 ] (order may vary)

Notice the require or import statement uses just 'lodash'. Do not add a path like './lodash' unless you have a local file. The module name is enough because Node.js looks in node_modules.

Solution 4: Check Your package.json

Your package.json file should list Lodash under dependencies or devDependencies. Open the file and look for this:


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

If it is missing, run npm install lodash --save to add it. This ensures that when others clone your project, they get Lodash too.

Solution 5: Clear npm Cache

A corrupted npm cache can cause module errors. Clearing it might help. Run this command:


npm cache clean --force

Then reinstall Lodash with npm install. This step is safe and often resolves stubborn issues.

Solution 6: Check Node.js Version

Lodash works with most Node.js versions. But an outdated Node.js might cause problems. Check your version with:


node --version

If you are using a very old version (below 10), consider upgrading. Download the latest LTS version from the official Node.js website. Older versions may have module resolution bugs.

Solution 7: Use the Correct File Path

If you are using a relative path, ensure it is correct. For example, if you have a local Lodash file, your import might look like this:


// Incorrect - will cause error
const _ = require('./lodash');

// Correct - if lodash is in node_modules
const _ = require('lodash');

Only use a relative path if you have a custom file named lodash.js in your project. Otherwise, stick to the module name.

Solution 8: Check for Typing Errors

A simple typo can cause this error. Double-check your import statement. Common mistakes include:

  • Writing lodash as lodashh or lodah.
  • Using capital letters like Lodash (Node.js module names are case-sensitive on some systems).
  • Adding extra spaces or special characters.

Always use lowercase lodash in your import.

Preventing the Error in the Future

To avoid this error again, follow these best practices:

  • Always run npm install after cloning a project.
  • Keep your package.json and package-lock.json files in version control.
  • Do not delete the node_modules folder manually unless necessary.
  • Use a .gitignore file to exclude node_modules from your repository.

If you encounter similar issues with other modules, check out our guide on how to Fix Node.js Error: Cannot find module. This resource covers general solutions for missing module errors.

Debugging with Code Examples

Let's walk through a complete example. Suppose you have a file app.js that uses Lodash. Here is what it might look like:


// app.js
const _ = require('lodash');

// Use lodash to merge two objects
const obj1 = { a: 1, b: 2 };
const obj2 = { c: 3, d: 4 };
const merged = _.merge(obj1, obj2);

console.log(merged);
// Expected output: { a: 1, b: 2, c: 3, d: 4 }

If you run node app.js and get the error, follow these steps:

  1. Check if node_modules/lodash exists. Run ls node_modules/lodash in your terminal.
  2. If it does not exist, install Lodash with npm install lodash.
  3. Run the file again. The output should be the merged object.

Here is the terminal output after a successful fix:


$ node app.js
{ a: 1, b: 2, c: 3, d: 4 }

This simple test confirms your setup works.

Common Mistake: Forgetting to Install

Many beginners forget to install dependencies after cloning a project. Always run npm install first. This command reads your package.json and downloads all required modules, including Lodash.

Remember, the node_modules folder is not committed to version control. So you must install it locally.

Conclusion

The "Cannot find module 'lodash'" error is easy to fix. The main solutions are installing Lodash, reinstalling dependencies, and checking your import path. Always ensure your package.json lists the module and that you run npm install after cloning a project.

By following the steps in this guide, you can resolve this error quickly. For more help with module errors, see our article on Fix Node.js Error: Cannot find module. Keep your dependencies updated and your code clean, and you will avoid most issues.

Happy coding!