Last modified: Jun 24, 2026

Fix Cannot Find Module Redis Error

Getting the error "Cannot find module 'redis'" in Node.js is common. This error stops your app from starting. It means Node.js cannot locate the Redis client library. This guide will help you fix it quickly.

We will cover the main causes and solutions. You will learn how to install, configure, and verify the Redis module. We will also show common mistakes and how to avoid them.

What Causes This Error?

The error occurs when your project lacks the redis package. Node.js needs this module to connect to a Redis server. The package is not part of Node.js core. You must install it separately.

Other reasons include wrong file paths or missing dependencies. Sometimes the node_modules folder is incomplete. This can happen after cloning a project or moving files.

Step 1: Install the Redis Module

The first fix is to install the redis package. Use npm or yarn. Run this command in your project root.


npm install redis

Or if you use yarn:


yarn add redis

This command downloads the package and adds it to node_modules. It also updates package.json and package-lock.json.

After installation, try running your app again. If the error persists, check the next steps.

Step 2: Check Your Import Statement

Make sure you import the module correctly. Node.js uses require or import. The syntax must match your project setup.

For CommonJS (default in Node.js), use:

 
// CommonJS syntax
const redis = require('redis');

For ES modules (if you set "type": "module" in package.json), use:

 
// ES module syntax
import redis from 'redis';

Double-check the spelling. Redis is case-sensitive. Use lowercase 'redis'.

Step 3: Verify node_modules Folder

Sometimes the node_modules folder is missing or corrupted. Delete it and reinstall all dependencies.


# Remove node_modules
rm -rf node_modules

# Reinstall all packages
npm install

This ensures a clean installation. It fixes issues from partial downloads or conflicts.

Step 4: Check package.json

Your package.json file must list the redis dependency. If it is missing, add it manually or reinstall.

Open package.json and look under "dependencies":

 
{
  "dependencies": {
    "redis": "^4.6.7"
  }
}

If the entry is missing, run npm install redis --save. This adds it automatically.

Step 5: Check Node.js Version

Older Node.js versions may not support the latest Redis client. The redis module v4 requires Node.js 12 or higher. Check your Node version.


node --version

If your version is below 12, upgrade Node.js. Download the latest LTS from the official website.

Step 6: Use a Specific Version

If the latest version causes issues, install a specific version. Some projects work better with older versions.


npm install [email protected]

Version 3 is stable and widely used. Test with your code to see if the error goes away.

Step 7: Global vs Local Installation

Installing Redis globally can cause confusion. Always install it locally in your project. Global modules are not accessible by require.

To install locally, omit the -g flag. Check that you are in the correct project folder.

Step 8: Check Your File Path

Make sure your script is in the right directory. The require('redis') looks in node_modules of the current folder. If your script is in a subfolder, Node.js still searches from the project root.

You can also use an absolute path, but this is not recommended. Stick to relative or bare module names.

Example: Simple Redis Connection

Here is a working example to test your setup.

 
// app.js
const redis = require('redis');

// Create a Redis client
const client = redis.createClient({
  url: 'redis://localhost:6379'
});

// Handle connection events
client.on('connect', () => {
  console.log('Connected to Redis');
});

client.on('error', (err) => {
  console.error('Redis error:', err);
});

// Connect to the server
client.connect().then(() => {
  console.log('Client connected');
});

Run this file with node app.js. If you see "Connected to Redis", the module is working.

Output Example

Here is the expected output when everything is correct.


Connected to Redis
Client connected

If you still see the error, check your Redis server. Make sure it is running on localhost:6379.

Common Mistakes to Avoid

Mistake 1: Forgetting to install the module. Always run npm install redis before using it.

Mistake 2: Using wrong import syntax. Match your project's module system (CommonJS or ES).

Mistake 3: Ignoring package.json. The dependency must be listed there.

Mistake 4: Not cleaning node_modules. A corrupted folder can cause many errors.

How to Prevent This Error

Always include redis in your package.json. Use a version lock to avoid breaking changes. Run npm install after cloning a project. This ensures all dependencies are present.

Also, use a .gitignore file to exclude node_modules. This keeps your repository clean and forces fresh installs.

Related Fixes

If you encounter similar errors with other packages, read our guide on Fix Node.js Error: Cannot find module. It covers general troubleshooting steps for missing modules.

Conclusion

The "Cannot find module 'redis'" error is easy to fix. Install the package, check your import, and verify your project setup. Always use local installations and keep your dependencies updated.

With these steps, you can resolve the error and connect to Redis smoothly. Test your code with the example provided. If you still face issues, double-check your Node.js version and Redis server status.

Remember to keep your package.json accurate and your node_modules clean. This prevents many common Node.js errors.