Skip to main content
  1. About
  2. For Teams
Asked
Modified 8 days ago
Viewed 45 times
0

I have the following code

const session = require("express-session")
const { createClient } = require("redis");
const RedisStore = require("connect-redis").default;

// Initialize Redis client
let redisClient = createClient({ url: process.env.REDIS_URL });
redisClient.connect().catch(console.error);

let redisStore = new RedisStore({
  client: redisClient,
  prefix: "test:",
});

app.use(
    session({
      store: redisStore,
      secret: process.env.SESSION_SECRET,
      resave: false,
      saveUninitialized: false,
      cookie: {
        secure: process.env.NODE_ENV === "production",
        httpOnly: true,
        maxAge: 1000 * 60 * 60 * 24, // 1 day
      },
    })
);

I'm trying to use connect-redis v9.0 but I get the error

let redisStore = new RedisStore({
                 ^

TypeError: RedisStore is not a constructor

What am I doing wrong?

I recently upgraded my node js version to the latest one so I'm guessing it has to do with that.

0

1 Answer 1

2
const RedisStore = require("connect-redis").default;

require("connect-redis").default will import the default object in RedisStore. A simple console.log(RedisStore ) will help you see its content.

If you wish to use this way, below code will help:

let redisStore = new RedisStore.RedisStore({...});

But a better way is to only import stuff you require and for that const { RedisStore } = require('connect-redis'); will help.

Here you are specifically importing RedisStore from the default object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.