Last modified: Jun 24, 2026
Fix ioredis Module Not Found Error
Encountering the error Cannot find module 'ioredis' in Node.js can stop your development work. This error happens when your project cannot locate the ioredis package. ioredis is a popular Redis client for Node.js. It helps you connect to Redis databases easily. This guide explains why this error occurs and how to fix it quickly.
We will cover common causes, step-by-step solutions, and best practices. Whether you are a beginner or an experienced developer, this article will help you resolve the issue. Let's dive in.
What Causes the ioredis Module Error?
The error usually appears when you run a Node.js script that uses ioredis. The message looks like this:
Error: Cannot find module 'ioredis'
Require stack:
- /home/user/project/app.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:102:18)
at Object. (/home/user/project/app.js:1:17)
This error means Node.js cannot find the ioredis package in your project's node_modules folder. Common reasons include:
- You forgot to install ioredis using npm or yarn.
- You are running the script from the wrong directory.
- The package is installed globally, not locally.
- Your
package.jsonis missing the dependency. - There is a typo in the module name.
Understanding these causes helps you fix the error faster. Let's look at each solution.
Solution 1: Install ioredis Locally
The most common fix is to install ioredis in your project. Open your terminal and navigate to your project folder. Then run:
npm install ioredis
If you use yarn, run:
yarn add ioredis
This command adds ioredis to your node_modules folder. It also updates your package.json file. After installation, try running your script again. The error should disappear.
Always install packages locally unless you have a specific reason. Local installations keep your project self-contained. This avoids conflicts with other projects.
Solution 2: Check Your Working Directory
Sometimes you run the script from a different folder. Node.js looks for modules in the current directory's node_modules folder. If you are in the wrong place, it cannot find ioredis.
Use the ls or dir command to check your current path. Ensure you are in the root of your project. The root contains the package.json file. If not, change to the correct directory:
cd /path/to/your/project
Then run your script again. This simple step often solves the problem.
Solution 3: Verify package.json
Open your package.json file. Look for ioredis in the dependencies section. It should look like this:
{
"dependencies": {
"ioredis": "^5.3.0"
}
}
If ioredis is missing, add it manually or run the install command again. If the version is wrong, update it. Use npm update ioredis to get the latest version.
Also, check for typos. The module name is ioredis, not io-redis or ioredis with a capital letter. Node.js is case-sensitive on some systems.
Solution 4: Delete node_modules and Reinstall
Sometimes the node_modules folder gets corrupted. A clean reinstall fixes this. Delete the folder and the package-lock.json file:
rm -rf node_modules package-lock.json
Then reinstall all dependencies:
npm install
This forces npm to fetch fresh copies of all packages, including ioredis. After this, test your script again.
Solution 5: Use Global Installation (Not Recommended)
You can install ioredis globally with npm install -g ioredis. However, this is not a good practice. Global packages can cause version conflicts between projects. They also make your project less portable.
Only use global installation for command-line tools, not for libraries like ioredis. If you already installed globally, you can still link it locally:
npm link ioredis
But it is better to install locally as shown in Solution 1.
Solution 6: Check Node.js Version Compatibility
ioredis requires a recent version of Node.js. Check your Node.js version with node --version. If you use an old version, ioredis might not work. Update Node.js to version 14 or higher.
You can use a version manager like nvm to switch versions easily. This ensures compatibility with modern packages.
Solution 7: Clear npm Cache
A corrupted npm cache can cause module errors. Clear the cache and reinstall:
npm cache clean --force
npm install
This removes cached data and forces a fresh download. It helps when the error persists after other fixes.
Example Code Using ioredis
Here is a simple example to test if ioredis works. Create a file called test-redis.js:
// Import ioredis
const Redis = require('ioredis');
// Create a Redis client
const redis = new Redis({
host: 'localhost',
port: 6379
});
// Test connection
redis.set('key', 'Hello Redis!')
.then(() => {
return redis.get('key');
})
.then((value) => {
console.log('Value:', value);
process.exit(0);
})
.catch((err) => {
console.error('Error:', err);
process.exit(1);
});
Run it with:
node test-redis.js
If ioredis is installed correctly, you should see output like:
Value: Hello Redis!
If you still get the error, double-check your installation steps.
Preventing the Error in Future Projects
To avoid this error, always follow these steps when starting a new Node.js project:
- Initialize your project with
npm init -y. - Install dependencies with
npm install ioredis. - Run scripts from the project root directory.
- Use a
.gitignorefile to excludenode_modules.
These habits keep your projects clean and error-free. For more help with module errors, check out our guide on Fix Node.js Error: Cannot find module.
Conclusion
The error Cannot find module 'ioredis' is common but easy to fix. Start by installing ioredis locally with npm install ioredis. Check your working directory and package.json file. If the problem continues, delete node_modules and reinstall. Always use local installations for better project management. With these steps, you can resolve the error and get back to coding. Remember to test with the example code provided. Happy coding!