skip to Main Content
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

2

Answers


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

    Login or Signup to reply.
  2. 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",
      })
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search