Last updated: Mar 6, 2024
Reading timeยท2 min
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
.
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.
npm install lodash # ๐๏ธ only if you use TypeScript npm install --save-dev @types/lodash
This will add the lodash
package to the dependencies of your project.
Now you should be able to import and use the package.
import _ from 'lodash'; console.log(_.add(100, 200)); // ๐๏ธ 300
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
.
{ "compilerOptions": { "types": [ "node" ] }, }
lodash
module.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
.
# 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.
# 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
lodash
is in your dependencies
objectIf the "Cannot find module 'lodash'" error persists, open your package.json
file and make sure it contains the lodash
package in the dependencies
object.
{ // ... 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
.
npm install
Or install the latest version of the package:
npm install lodash@latest # ๐๏ธ only if you use TypeScript npm install --save-dev @types/lodash@latest