0
Ubuntu Mate 22.04.2 lts
Node 18.16.0
Redis 7.0.11 - without password (no auth), ping-pong test passes
"body-parser": "^1.20.2"
"connect-redis": "^7.1.0"
"cors": "^2.8.5"
"dotenv": "^16.3.1"
"express": "^4.18.2"
"express-session": "^1.17.3"
"redis": "^4.6.7"

I am trying to run the example - https://devdotcode.com/how-to-manage-session-in-nodejs-using-redis-store/ And i get the error:

const redisStore = require('connect-redis')(session);
                                           ^
TypeError: require(...) is not a function
at Object.<anonymous> (/home/dol/MyPrograms/redis03/index.js:19:44)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47

Second example - https://medium.com/swlh/session-management-in-nodejs-using-redis-as-session-store-64186112aa9 Error:

const RedisStore = connectRedis(session)
                   ^
TypeError: connectRedis is not a function
at Object.<anonymous> (/home/dol/MyPrograms/redis01/index.js:11:20)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47

What am I doing wrong?

I tried to understand the documentation - https://developer.redis.com But there are too complicated examples

Doleensk
  • 1
  • 1

5 Answers5

1

I see you solved this issue by using version 6 of connect-redis, in the case of version 7, try this:

const session = require("express-session");
const redis = require("redis");
const RedisStore = require("connect-redis").default;
const redisClient = redis.createClient({
    url: "redis://redis:6379",
});

redisClient.connect().catch(console.error);



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

app.use(
   session({
     store: redisStore,
     secret: "SESSION_SECRET",
     resave: false,
     saveUninitialized: false,
     cookie: {
       secure: false,
    },
  })
);
PELUMI
  • 11
  • 1
0

please check whether you are opening (starting) redis port..

adhil
  • 1
0

Error message is correct. In your example redisStore is not a function, it's a class - based on a current documentation on npm https://www.npmjs.com/package/connect-redis

Try this:

const RedisStore = require('connect-redis');
const session = require('express-session');
const { createClient } = require('redis');

const redisClient = createClient();
redisClient.connect().catch(console.error);

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

app.use(
  session({
    store: redisStore,
    resave: false,
    saveUninitialized: false, 
    secret: "keyboard cat",
  })
)
ethArek
  • 33
  • 4
  • Thanks for the answer but this example doesn't work – Doleensk Jun 23 '23 at 14:03
  • const redisStore = new RedisStore({ ^ TypeError: RedisStore is not a constructor at Object. (/home/dol/MyPrograms/redis02/index.js:8:20) at Module._compile (node:internal/modules/cjs/loader:1254:14) at Module._extensions..js (node:internal/modules/cjs/loader:1308:10) at Module.load (node:internal/modules/cjs/loader:1117:32) at Module._load (node:internal/modules/cjs/loader:958:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:23:47 – Doleensk Jun 23 '23 at 14:04
  • please try importing RedisStore this way: const RedisStore = require("connect-redis").default; – ethArek Jun 23 '23 at 16:09
  • This didn't help either. – Doleensk Jun 24 '23 at 05:12
0

Like ethArek said above, after looking at the documentation the error should be expected, at first i forgot to install redis locally, the code provided by him (the correct way to connect to redis) worked just fine.

Make sure that Redis is listening on the correct address and port

const redisClient = createClient({
    host: 'localhost',
    port: 6379,
});
Ayoub-89
  • 1
  • 1
0

Follow the "Migration from v6" chapter of Release notes

Uninstalling of @types/connect-redis was helpfull for me.

Sander
  • 92
  • 10