How To Solve “Cannot find module ‘lodash'” In Javascript?

“Cannot find module ‘lodash'” in Javascript

To fix the error “Cannot find module ‘lodash'” in Javascript, you must have npm and lodash knowledge. So before we get into the main content, let’s find out what they are.

What is npm, and what is lodash?

NPM stands for Node Package Manager, a tool to create and manage javascript libraries for Nodejs. If you work with javascript regularly, you must have heard about it.

Lodash, like all other packages, is hosted on npm. Lodash’s predecessor is underscored – a well-known javascript library. Lodash provides many helpful functions for working with data types in JS, such as string functions, object handler functions, and array handler functions…

Why are you getting the error “Cannot find module ‘lodash'” in Javascript?

To use lodash, you must install it from npm. If you don’t have it installed but try to use lodash like the example below, the program will crash.

index.js file:

// import 'lodash' but not installed
const _ = require("lodash");

console.log(_.trim("#####learnshareIT#####", "#"));

// expected output: learnshareIT


Error: Cannot find module 'lodash'

How to fix this problem?

Install lodash before using it.

To fix this error, you just need to install lodash before using it. Open a command line interface in your project’s directory. And type:

npm install --save lodash

If you are working with typescript, then you must install @types/lodash by typing:

npm install --save @types/lodash

Next, check package.json:

  "name": "my-express",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  "dependencies": {
    "express": "^4.18.2",
    "lodash": "^4.17.21" // installed lodash

Like the above code, a lodash package in the dependencies proves you have successfully installed it. Now use it.

Let’s try the first example again, but this time we have installed lodash.

index.js file:

// import lodash
const _ = require("lodash");

console.log(_.trim("######learnshareIT#####", "#"));

// expected output: learnshareIT



Clear cache if lodash is installed.

If you have lodash installed and in the dependencies also have lodash but still get the error “Cannot find module ‘lodash'” in Javascript, just clear the npm cache.

First, you should delete the node_modules folder and the package-lock.js file. Then run the following two commands:

npm cache clean -force
npm install

Once done, the node_modules folder and the package_lock.json file will be reinstalled. Like this:

Now use lodash wherever you want, and you will never get the error message “Cannot find module ‘lodash'” again.


You just discovered two ways to fix the error “Cannot find module ‘lodash'” in Javascript. We believe that if you encounter the same error with another library other than lodash, you can still fix it on your own without google. If you have any questions, please comment below.

Have a great day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *