Search code examples
node.jsherokuredis

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379 Heroku


I have created a Nodejs server that uses Redis. It runs fine when I am running it in my machine. But I get the error above when I try to push the code to heroku. I already installed Redis togo and I can see the REDISTOGO_URL in my config variable.

Following some Stackoverflow posts I did:

let RedisStore = require('connect-redis')(session)

let redisClient = redis.createClient()
if(process.env.REDISCLOUD_URL){
    let redisURL = url.parse(process.env.REDISCLOUD_URL);
    redisClient = redis.createClient(redisURL)
}

My app runs fine in development but when I try to start my app in heroku I get the following error when I do heroku logs --tail :

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379


Solution

  • you got that error is because you first try to create redis client with redis.createClient(). by default, it will connect to 127.0.0.1:6379.

    It works fine on localhost because it won't go into the if (process.env.REDISCLOUD_URL and the local redis is available.

    To fix this,

    let redisClient
    if(process.env.REDISCLOUD_URL){
        let redisURL = url.parse(process.env.REDISCLOUD_URL);
        redisClient = redis.createClient(redisURL)
    } else {
        redisClient = redis.createClient()
    }