Last modified: Jun 28, 2026
Fix Cannot find module 'mocha'
When you run tests in Node.js and see the error "Cannot find module 'mocha'", it stops your work. This is a common problem for beginners. The error means Node.js cannot locate the Mocha testing library. Luckily, the fix is simple. In this guide, we will explain why this happens and how to solve it. We will use short steps and clear examples.
Mocha is a popular JavaScript test framework. It helps you write and run tests for your code. If you get this error, your project likely lacks the Mocha package. Or, it may be installed in the wrong place. Let's explore the causes and solutions.
Why Does This Error Occur?
The error "Cannot find module 'mocha'" appears for a few reasons. First, you may have not installed Mocha at all. Second, you might have installed it globally instead of locally. Third, your package.json file may miss the dependency. Fourth, your node_modules folder could be corrupted. Each cause has a direct fix. We will cover them one by one.
Before fixing, check if Mocha is installed. Run this command in your terminal:
npm list mocha
If you see "(empty)" or an error, Mocha is missing. If you see a version number, Mocha is installed but maybe not accessible. Let's move to the solutions.
Solution 1: Install Mocha Locally
The best practice is to install Mocha as a development dependency. This keeps your project self-contained. Run this command in your project folder:
npm install --save-dev mocha
This adds Mocha to your package.json under devDependencies. It also installs it in node_modules. Now, your tests can find Mocha. Try running your test again:
npx mocha
Using npx ensures you run the local version. If you used a global install before, remove it first with npm uninstall -g mocha to avoid conflicts.
Solution 2: Check Your package.json
Sometimes, your package.json may list Mocha but it is not installed. Open the file and look for "mocha" under devDependencies. If you see it, run npm install to install all dependencies. This command reads package.json and installs everything. Here is an example package.json snippet:
{
"name": "my-test-project",
"version": "1.0.0",
"devDependencies": {
"mocha": "^10.0.0"
},
"scripts": {
"test": "mocha"
}
}
After running npm install, the node_modules folder should contain Mocha. If not, delete node_modules and package-lock.json, then run npm install again. This rebuilds everything fresh.
Solution 3: Use the Correct Path
If you are running tests from a subfolder, Node.js may look for Mocha in the wrong place. Always run the mocha command from the root of your project. If you use a test script in package.json, use npm test instead. This ensures the path is correct. For example, your package.json scripts section should look like this:
"scripts": {
"test": "mocha test/*.js"
}
Now run npm test from the project root. This avoids path issues. If you still get the error, check if your node_modules folder exists in the same directory as package.json.
Solution 4: Clear npm Cache and Reinstall
A corrupted npm cache can cause module errors. Clear the cache first:
npm cache clean --force
Then delete node_modules and package-lock.json:
rm -rf node_modules package-lock.json
Finally, reinstall all dependencies:
npm install
This gives you a clean slate. After this, install Mocha again if needed: npm install --save-dev mocha. This method fixes many weird module errors.
Solution 5: Global vs Local Installation
If you installed Mocha globally with npm install -g mocha, your project may not find it. Global installs are for command-line tools you use everywhere, but they are not recommended for project dependencies. To fix this, uninstall the global version and install locally:
npm uninstall -g mocha
npm install --save-dev mocha
Now your project has its own copy. This prevents version conflicts and makes your project portable. If you need a global Mocha for other projects, consider using npx instead. It runs the local version automatically.
Example: Running a Simple Test
Let's create a quick test to verify Mocha works. First, install Mocha locally as shown above. Then create a file named test.js with this code:
// test.js - A simple Mocha test
const assert = require('assert');
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when value is not present', function() {
assert.strictEqual([1, 2, 3].indexOf(4), -1);
});
});
});
Now run the test:
npx mocha test.js
You should see output like this:
Array
#indexOf()
✓ should return -1 when value is not present
1 passing (5ms)
If you see this, Mocha works. If you get the "Cannot find module" error again, go back to the solutions above. Double-check your installation and path.
Additional Tips
Always use npx mocha instead of just mocha. This ensures you use the local version. Also, add Mocha to your devDependencies in package.json so other developers can install it with npm install. If you work on multiple projects, each should have its own Mocha installation. For more help with Node.js module errors, read our guide on Fix Node.js Error: Cannot find module. It covers similar issues with other packages.
Another common mistake is running tests from a different directory. Always navigate to your project root first. Use cd /path/to/your/project before running npm test. If you use a test runner like mocha --recursive, ensure all test files are in the specified folder.
Conclusion
The "Cannot find module 'mocha'" error is easy to fix. The main steps are: install Mocha locally with npm install --save-dev mocha, check your package.json, use npx mocha, and clear the cache if needed. Always run tests from the project root. By following these steps, you can get your tests running quickly. Remember to keep your dependencies updated and use local installations for each project. This prevents future errors and makes your code more reliable. Happy testing!