How to solve Cannot find module 'yargs' error?

The error "Cannot find module 'yargs'" occurs when you use yargs package in your code without installing it. If you want to know whether yargs is installed or not in your project then run the following command:

$npm list --depth=0

If you don't find yargs in the output it means yargs is not installed and you have to install it by running npm install yargs command.

On the other hand, if yargs is present in the output, it means yargs is installed. The next step, in this case, is to check the package.json file. Open the package.json file and look for the dependencies section in it. If you find something like this:

package.json
...
"dependencies":{
  ...
  "yargs":"^17.2.1"
  ...
}
...

It means yargs is installed in your project. The version number of yargs could be anything. After this, go to the node_modules folder and check yargs folder is present or not. If it is present, it means yargs is installed.

Even after doing this, if you still get this error, remove node_modules folder and package-lock.json file (not package.json) and run npm install command again.

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

$npm install

After doing this, you will find that this error is solved, and you are able to use yargs package in your project.

Note: To solve the error "Cannot find module 'yargs'", install yargs package by running the following command: npm install yargs. This command installs yargs package in your project. You can verify that yargs is installed by running npm list --depth=0 command on the terminal.

Recommended Posts