How to solve Cannot find module 'commander' error in Node.js?

The error "Cannot find module 'commander'" occurs when you use the commander package in your code without installing it in your project.

Cannot find module 'commander' error

To solve this error, open the terminal or command prompt and install the commander module by running the following command:

$npm install commander

After installing it, restart your IDE.

Sometimes even after doing this process, the error is not resolved. In that case, delete node_modules and package-lock.json file, clean npm cache, and then run npm install command.

Note: Don't accidentally remove package.json. You need to delete package-lock.json file, not package.json.
$rm -rf node_modules
$rm -f package-lock.json
$npm cache clean --force
$npm install
$nm install commander

After this, open the package.json file and ensure that the commander package is present in the dependencies property.

package.json
{
  ...
  "dependencies": {
    "commander":"^9.3.0"
  },
  ...
}

If the commander is not in the package.json file, install its latest version.

$npm install commander

Recommended Posts