Cannot find module 'lodash' error [Solved]

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
2 min

banner

# Cannot find module 'lodash' error [Solved]

To solve the error "Cannot find module 'lodash'", make sure to install the lodash package by opening your terminal in your project's root directory and running the following command: npm i lodash.

If you use TypeScript, install the typings by running npm i -D @types/lodash.

shell
Error Cannot find module 'lodash' [ERR_MODULE_NOT_FOUND]: Cannot find package 'lodash'

Open your terminal in your project's root directory (where your package.json file is located) and run the following command.

shell
npm install lodash # ๐Ÿ‘‡๏ธ only if you use TypeScript npm install --save-dev @types/lodash

npm install lodash

This will add the lodash package to the dependencies of your project.

Now you should be able to import and use the package.

index.js
import _ from 'lodash'; console.log(_.add(100, 200)); // ๐Ÿ‘‰๏ธ 300

import and use lodash module

If you use TypeScript and get the "Cannot find module 'lodash' or its corresponding type declarations" error, open your tsconfig.json file and make sure the types array contains the string node.

tsconfig.json
{ "compilerOptions": { "types": [ "node" ] }, }
This should fix the error and now TypeScript should be able to find the type definitions for the lodash module.

# Delete your node_modules and reinstall your dependencies

If the error is not resolved, try to delete your node_modules and package-lock.json (not package.json) files, re-run npm install and restart your IDE.

If you are on macOS or Linux, issue the following commands in bash or zsh.

shell
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

If you are on Windows, issue the following commands in CMD.

cmd
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install
Make sure to restart your IDE and dev server if the error persists. VSCode often glitches and needs a reboot.

# Verify lodash is in your dependencies object

If the "Cannot find module 'lodash'" error persists, open your package.json file and make sure it contains the lodash package in the dependencies object.

package.json
{ // ... rest "dependencies": { "lodash": "^4.17.21", }, "devDependencies": { // ๐Ÿ‘‡๏ธ only if you use TypeScript "@types/lodash": "^4.14.180", } }

You can try to manually add the line and re-run npm install.

shell
npm install

Or install the latest version of the package:

shell
npm install lodash@latest # ๐Ÿ‘‡๏ธ only if you use TypeScript npm install --save-dev @types/lodash@latest

install latest version of lodash module

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev