Node Redis does not work on my windows computer even though the server is up and running
Asked Answered
C

13

8
const express = require("express");
const redis = require("redis");
const app = express();

const client = redis.createClient({
  url: "redis://[email protected]",
});

client.on("connect", function () {
  console.log("redis connected");
  console.log(`connected ${redisClient.connected}`);
});

client.on("error", (err) => {
  console.log(err);
});

app.listen(process.env.PORT || 3000, () => {
  console.log("Node server started");
});

The above code does not show any connection to redis server even though I have checked the EC2 redis instance by connecting using Redsmin. hosting details in Redsmin

This is a very simple thing to do but the error that I get cannot be googled.

Node server started C:\Users\Sithira_105661\Desktop\Projects\Learning\Redis\node_modules@node-redis\client\dist\lib\client\index.js:387 return Promise.reject(new errors_1.ClientClosedError()); ^

ClientClosedError: The client is closed at Commander._RedisClient_sendCommand (C:\Users\Sithira_105661\Desktop\Projects\Learning\Redis\node_modules@node-redis\client\dist\lib\client\index.js:387:31) at Commander.commandsExecutor (C:\Users\Sithira_105661\Desktop\Projects\Learning\Redis\node_modules@node-redis\client\dist\lib\client\index.js:160:154) at Commander.BaseClass. [as set] (C:\Users\Sithira_105661\Desktop\Projects\Learning\Redis\node_modules@node-redis\client\dist\lib\commander.js:8:29) at Object. (C:\Users\Sithira_105661\Desktop\Projects\Learning\Redis\redis.js:19:8) at Module._compile (node:internal/modules/cjs/loader:1101:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:17:47

Help me understand the issue. Thanks in advance.

Casanova answered 28/11, 2021 at 17:23 Comment(1)
can you first make sure that the server is reachable with the following CLI cmd: redis-cli -u redis://<url>Maryannemarybella
C
28

finally found the solution. I used node-redis 3.0.0 rather than 4 and the code works fine. I do not know why it does not work in latest node-redis . If any of you guys are getting this issue use node-redis 3

Casanova answered 28/11, 2021 at 17:41 Comment(5)
thank you bro. I was struggling for 3 days and I was about to go crazy. Even though I did everything completely right, it just wasn't working. and finally "npm i [email protected]" its workkkkkMien
Your welcome. I was also struggling with this but luckily stumbled upon the solution.Casanova
Almost pulled my hair out, thanks so muchGaekwar
Major version "3" is not supporting the Hash commandsSlater
Damn you save my life and this redis so painful don't tell us any thing.Diminutive
R
10

I used node-redis 3.1.2 rather than 4 and the code works fine.

Rommel answered 1/12, 2021 at 10:52 Comment(0)
T
6

I don't thing we should decrease the version from 4 to 3.

we can use -

const redis = require('redis');

const client = redis.createClient();

   
(async () => {
    await client.connect();
})();

client.on('connect', () => console.log('Redis Client Connected'));
client.on('error', (err) => console.log('Redis Client Connection Error', err));
Tincher answered 21/12, 2021 at 5:48 Comment(0)
D
4

If you're using redis ^4.0.0, you can do this:

const { createClient } = require('redis');
const { REDIS_HOST_URL } = require('../config');

const client = createClient({ url: REDIS_HOST_URL });

(async () => {
    await client.connect();
})();

client.on('connect', () => console.log('::> Redis Client Connected'));
client.on('error', (err) => console.log('<:: Redis Client Error', err));

You can read more on their documentation here.

Diploblastic answered 10/12, 2021 at 15:35 Comment(0)
H
2

Please check your ec2 redis connection

    const client = redis.createClient({
       url: "redis://username:password@ec2_endpoint:port",
     });

Connection string format: redis[s]://[[username][:password]@][host][:port]

Heiduc answered 28/11, 2021 at 17:30 Comment(0)
P
2

You are using a very old version of Redis, an abandoned project by Microsoft called OpenTech Redis.

I suggest you checkout Memurai. Memurai is an up-to-date native Windows port of Redis that derives from that project (see this commit).

Memurai is also available with a free Developer Edition.

Disclaimer: I work in Memurai.

Plumose answered 2/12, 2021 at 15:45 Comment(0)
W
1

In version 4.0.1, we have to add .connect() to run redis.

They have migration guide

This is my code, but it seems to not work well.

async function connectRedis(){

    try {

        const redisURL = '<link-redis-provider>';

        const client = createClient({
           url: redisURL
        });

        await client.connect();
        console.log('Cache is ready');

    } catch (error) {
       console.log('Connect to cache is failed')
    }
}
module.exports = {connectRedis}
Washerwoman answered 24/12, 2021 at 8:40 Comment(0)
B
1

Change to Version 3.1.2

  1. remove node_modules and package-lock
  2. open package.json
  3. edit redis to version 3.1.2 and save it
  4. run npm install

if you just want to install direct :

npm i [email protected]
Bend answered 12/5, 2022 at 9:3 Comment(0)
B
0
const redis = require('redis');
const client = redis.createClient({
    host: '<hostname>',
    port: <port>,
    password: '<password>'
});

client.on('error', err => {
    console.log('Error ' + err);
});

you can read detail at here https://docs.redis.com/latest/rs/references/client_references/client_nodejs/

Bookbinding answered 16/3, 2022 at 8:54 Comment(0)
S
0

i think the issue may be due to v4+ using typescript in source code. if your server code is not typescript compatible. could be the issue .

Shel answered 14/7, 2022 at 10:3 Comment(0)
H
0

I had a similar experience. I attributed the cause of the issue with the redis server I am using which is older than 5.0. So, perhaps you are in a similar situation.

According to the node-redis documentation,

"Node Redis should work with older versions of Redis, but it is not fully tested and we cannot offer support." However, "< 5.0" is marked not supported.

Hager answered 11/1, 2023 at 14:33 Comment(0)
M
0

install the redis client 3.1.2 just put 3.1.2 in dependencies in package.json next to redis

"dependencies" : { "redis": "3.1.2" }

Melda answered 23/1, 2023 at 2:25 Comment(0)
C
0

Most of the previous answers point in the correct direction. However, one minor change which caused me an hour to figure out was semi-colon and parenthesis.

Error:


(async () => { client.on('error', (err) => console.log('Redis Client Error', err)); })
    ^
    
TypeError: (intermediate value)(...) is not a function

It was happening due to change in redis package of nodejs. From 4.x.x versions, the API to connect database has significantly changed.

The correct syntax now is:

(async () => { client.on('error', (err) => console.log('Redis Client Error', err)); })();

Inside immediately Invoked Function Expressions (IIFE) require parenthesis to invoke them.

PS: async and await are necessary as Node Redis now uses native Promises by default for all functions.

Documentation: https://github.com/redis/node-redis/blob/master/docs/v3-to-v4.md

Cumberland answered 22/3, 2023 at 11:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.