Nabil Ahmed
Nabil Ahmed

Reputation: 29

require("connect-redis")(session); require(...) is not a function

I was building a real-time chat app. I am using express-session to add cookies so that users don't have to login every time page refreshes.

const session = require("express-session");
const Redis = require("ioredis");
const RedisStore = require("connect-redis")(session);

This is my import. I have more but I think these are related to the problem so I am sharing only these. And I have all of this downloader in my project in case anyone says to double check if I installed them properly or not that's why I am adding this. This is how I was implementing the code ...

const redisClient = new Redis();
app.use(session({
    secret:process.env.COOKIE_SECRET,
    credentials:true,
    name:"$id",
    store: new RedisStore({client:redisClient}), //This is the user's session information
    resave:false,
    saveUninitialized:false,
    cookie: {
        secure:process.env.ENVIRONMENT === "production" ? "true" : "auto",
        httpOnly:true,
        expires: 1000 * 60 * 60 * 24 * 7,
        sameSite:process.env.ENVIRONMENT === "production" ? "none" : "lax",
    }
}))

But I keep this error message that require(...) is not a function. I tried to solve it but I couldn't.

I tried to install redis instead of ioredis to see if this solves but it did not.

Upvotes: 0

Views: 1858

Answers (2)

Anu
Anu

Reputation: 28

I have the same issue and I want to investigate it later. But for now I downgraded connect-redis verion (as below) to version 6 and the error stopped.

 "dependencies": {
    "connect-redis": "^6.0.0",

Upvotes: 0

Gordon  Mullen
Gordon Mullen

Reputation: 21

The npm page for connect-redis says to use .default instead of (session) when you require the RedisStore:

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

After I used default everything spun up properly

Upvotes: 2

Related Questions