skip to Main Content

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

3

Answers


  1. Your code seems to be expecting a REDISCLOUD_URL environment variable. If you’ve defined REDISTOGO_URL then that would explain why it isn’t working.

    Login or Signup to reply.
  2. 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()
    }
    
    Login or Signup to reply.
  3. Heroku requires you to provide a REDISCLOUD_URL environment variable when creating redisClient.

    so you should provide the REDISCLOUD_URL while creating the Redis client.

    let redisClient
    if(process.env.REDISCLOUD_URL){
        redisClient = redis.createClient(process.env.REDISCLOUD_URL)
    } else {
        redisClient = redis.createClient()//for running locally
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search