Last modified: Jun 28, 2026
Fix Cannot find module commander Error
If you run a Node.js script and see the error Cannot find module 'commander', do not panic. This is a common issue that happens when the commander package is missing from your project. This guide will show you how to fix it quickly and prevent it from happening again.
The commander module is a popular Node.js library for building command-line interfaces (CLIs). Many tools and scripts rely on it. When Node cannot find it, your script will fail with a clear error message. Let's solve it step by step.
What Causes the Error?
The error appears when you try to require('commander') in your code, but the module is not installed. This can happen for several reasons:
- You forgot to run
npm installafter cloning a project. - The
package.jsonfile does not listcommanderas a dependency. - You installed the module globally but your script runs locally.
- You deleted the
node_modulesfolder accidentally.
Step 1: Install Commander Locally
The most common fix is to install commander as a local dependency. Open your terminal in the project root directory and run:
npm install commander
This command adds commander to your node_modules folder and updates package.json. After installation, try running your script again.
Step 2: Check Your package.json
Ensure that commander appears in the dependencies section of your package.json. If it is missing, the module will not be installed for other developers or when you deploy. Here is an example of a correct package.json entry:
{
"name": "my-cli-tool",
"version": "1.0.0",
"dependencies": {
"commander": "^11.0.0"
}
}
If the entry is missing, add it manually and run npm install again. This ensures the module is always available.
Step 3: Check for Global Installation
Some developers install commander globally with npm install -g commander. However, global modules are not automatically available to local scripts. To use a globally installed module, you must explicitly link it or install it locally. The safest approach is to always install commander locally in your project.
Step 4: Verify Your Node.js Code
Make sure your code uses the correct require statement. The typical way to import commander is:
// Import commander module
const { program } = require('commander');
// Define a simple command
program
.name('my-app')
.description('CLI to do amazing things')
.version('1.0.0');
program
.command('greet')
.description('Greet a user')
.argument('<name>', 'user name')
.action((name) => {
console.log(`Hello, ${name}!`);
});
program.parse();
If you run this script without installing commander, you will see the error. After installing, it should work correctly.
Step 5: Clear npm Cache (If Needed)
Sometimes, a corrupted cache can cause the error. To fix this, clear the npm cache and reinstall:
npm cache clean --force
rm -rf node_modules
npm install
This removes all cached data and reinstalls dependencies from scratch.
Example Error and Fix
Here is a typical error message you might see:
node:internal/modules/cjs/loader:1031
throw err;
^
Error: Cannot find module 'commander'
Require stack:
- /home/user/my-project/index.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1028)
...
After running npm install commander, the script runs without errors:
$ node index.js greet Alice
Hello, Alice!
Prevent the Error in the Future
To avoid this error in the future, follow these best practices:
- Always run
npm installwhen you clone a repository. - Commit your
package-lock.jsonfile to version control. - Use a
.gitignorefile to excludenode_modules. - Check your
package.jsonfor missing dependencies before sharing code.
If you encounter similar module errors, you might find our guide on Fix Node.js Error: Cannot find module helpful for other common modules.
Conclusion
The Cannot find module 'commander' error is easy to fix. Simply install the package locally with npm install commander, verify your package.json, and ensure your code uses the correct import. By following the steps in this article, you can resolve the issue quickly and keep your Node.js projects running smoothly. Remember to always check your dependencies before sharing or deploying your code.