Last modified: Jun 30, 2025 By Alexander Williams
Fix Error: Cannot find module 'jest'
If you see the error Cannot find module 'jest', don't worry. This is a common issue in Node.js projects. It means your system can't locate the Jest testing framework.
Table Of Contents
- Why does this error occur?
- Solution 1: Install Jest locally
- Solution 2: Install Jest globally
- Solution 3: Check your node_modules
- Solution 4: Verify your import statement
- Solution 5: Check your NODE_PATH
- Solution 6: Rebuild your dependencies
- Solution 7: Check for version conflicts
- Solution 8: Verify your package.json
- Common mistakes to avoid
- Conclusion
Why does this error occur?
The error appears when Node.js tries to require Jest but can't find it. This usually happens because:
- Jest isn't installed in your project
- It's installed in the wrong location
- Your project dependencies are corrupted
Solution 1: Install Jest locally
The simplest fix is to install Jest in your project. Run this command in your project root:
npm install jest --save-dev
This installs Jest as a dev dependency. The --save-dev
flag adds it to your package.json.
Solution 2: Install Jest globally
If you want to use Jest across multiple projects, install it globally:
npm install -g jest
After installation, verify it works by checking the version:
jest --version
Solution 3: Check your node_modules
Sometimes the module exists but Node can't find it. Try these steps:
- Delete node_modules folder
- Delete package-lock.json
- Run
npm install
again
Solution 4: Verify your import statement
Ensure you're importing Jest correctly. In your test file, use:
const jest = require('jest');
// Or for ES modules:
import jest from 'jest';
Solution 5: Check your NODE_PATH
Node.js looks for modules in specific paths. If Jest is installed but not found, check your NODE_PATH environment variable.
echo $NODE_PATH
If it's empty, you may need to set it. This issue is similar to the Cannot find module 'typescript' error.
Solution 6: Rebuild your dependencies
Corrupted dependencies can cause this error. Try:
npm rebuild
This reinstalls all your dependencies. It often fixes module-related issues.
Solution 7: Check for version conflicts
Version mismatches can cause problems. Ensure all Jest-related packages are compatible. This is similar to fixing the Cannot find module 'react' error.
Solution 8: Verify your package.json
Your package.json must list Jest as a dependency. Check the devDependencies section:
"devDependencies": {
"jest": "^29.0.0"
}
Common mistakes to avoid
When working with Jest, avoid these mistakes:
- Installing Jest in the wrong directory
- Forgetting to run npm install after adding Jest
- Having multiple versions of Jest installed
These issues can also occur with other modules like in the Cannot find module 'webpack' error.
Conclusion
The Cannot find module 'jest' error is usually easy to fix. Most often, it means Jest isn't installed or installed incorrectly. Follow the solutions above to resolve it quickly.
Remember to always check your project structure and dependencies. Proper installation and configuration will prevent most module-related errors in Node.js projects.