Last modified: Jun 28, 2026
Fix Cannot Find Module 'chai' Error
Are you stuck with the error "Cannot find module 'chai'" in your Node.js project? This is a common issue for beginners and experienced developers alike. The error usually appears when you try to run tests or import Chai, a popular assertion library. The good news? It is easy to fix once you understand the root cause.
In this article, we will break down what causes this error and show you step-by-step solutions. We will also include code examples and outputs to make the process clear. By the end, you will be able to resolve the issue quickly and move on with your project.
What Does This Error Mean?
The error "Cannot find module 'chai'" occurs when Node.js cannot locate the Chai package in your project's node_modules folder. This can happen for several reasons: the package is not installed, it is installed globally instead of locally, or there is a problem with your package.json file.
Here is an example of what the error looks like in your terminal:
Error: Cannot find module 'chai'
Require stack:
- /home/user/project/test.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object. (/home/user/project/test.js:1:1)
This output shows that Node.js tried to load Chai from test.js but could not find it. The error stack trace points to the exact file and line number, which helps with debugging.
Common Causes of the Error
Let us look at the most frequent reasons for this issue:
- Chai is not installed: You forgot to run
npm install chaiin your project. - Chai is installed globally: Global packages are not accessible from local projects.
- Wrong directory: You are running the script from a folder without a
node_modulesdirectory. - Corrupted
node_modules: The folder might be damaged or incomplete. - Missing
package.json: Your project lacks a proper dependency file.
How to Fix the Error
1. Install Chai Locally
The most straightforward solution is to install Chai as a local dependency. Open your terminal in the project root and run:
npm install chai --save-dev
The --save-dev flag adds Chai to your devDependencies in package.json. This is standard because Chai is typically used only for testing, not in production.
After installation, your package.json should have an entry like this:
{
"devDependencies": {
"chai": "^4.3.10"
}
}
2. Verify the Installation
Check if Chai is in your node_modules folder. Run this command:
ls node_modules | grep chai
If you see "chai" in the output, the package is installed correctly. If not, repeat step 1.
3. Run from the Correct Directory
Make sure you are executing your script from the project root. For example, if your project is in /home/user/my-project, navigate there first:
cd /home/user/my-project
node test.js
Running from a wrong folder will cause Node.js to search for modules in the wrong location.
4. Reinstall All Dependencies
If the error persists, your node_modules folder might be corrupted. Delete it and reinstall everything:
rm -rf node_modules
npm install
This command removes the entire node_modules folder and then reinstalls all dependencies from your package.json file. It is a safe and effective fix for many module-related errors.
5. Check for Global Installation
If you installed Chai globally with npm install -g chai, it will not be available in your local project. To fix this, install it locally as shown in step 1. You can check global packages with:
npm list -g --depth=0 | grep chai
If it shows up, uninstall the global version to avoid confusion:
npm uninstall -g chai
Example Code with Comments
Here is a simple test file that uses Chai. The comments explain each step:
// test.js - A simple test using Chai
const chai = require('chai'); // Import Chai module
const expect = chai.expect; // Use the expect interface
// A sample function to test
function add(a, b) {
return a + b;
}
// Test case
describe('add function', function() {
it('should return 5 for 2 + 3', function() {
const result = add(2, 3);
expect(result).to.equal(5); // Assert that result equals 5
});
});
When you run this file with node test.js, you should see the test output if Chai is installed correctly. If not, the error will appear.
Preventing the Error in the Future
To avoid this error in future projects, follow these best practices:
- Always use
npm initto create apackage.jsonfile before installing packages. - Install dependencies locally with
npm installinstead of globally. - Use a version control system like Git to track your
package.jsonandpackage-lock.jsonfiles. - Run
npm installimmediately after cloning a project to set up all dependencies.
For a broader understanding of similar issues, check out our guide on how to Fix Node.js Error: Cannot find module. This resource covers other common module errors and their solutions.
Conclusion
The "Cannot find module 'chai'" error is a simple fix that usually boils down to missing or improperly installed dependencies. By installing Chai locally, verifying your directory, and reinstalling if necessary, you can resolve it in minutes. Remember to always run npm install after cloning a project and use local packages for your code.
With the steps and examples in this guide, you should be back to writing tests without frustration. If you encounter other module-related errors, refer to our article on Fix Node.js Error: Cannot find module for more help. Happy coding!