skip to Main Content

I’m creating an API using NestJS and was trying to set up a session store for my express session but I get an error from this line. I did use express-session with Redis on a new project I created just using express beforehand to understand how Redis and express sessions worked but when I tried porting it over to NestJS it didn’t work.

Main.ts

import connectRedis from 'connect-redis';
import { redis } from './redis';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const RedisStore = connectRedis(session);

This is the error I get

const RedisStore = connectRedis(session);
                               ^
TypeError: connect_redis_1.default is not a function

The error occurs before any other redis related feature or express-session is called. I will still include how I set up Redis & Express-Session in case that is needed.

Redis.ts

import Redis from 'ioredis';

export const redis = new Redis(
  port,
  'hostName',
  { password: 'password' },
);

Session Inside of Main.ts

  app.use(
    session({
      store: new RedisStore({ client: redis }),
      cookie: {
        maxAge: 60000 * 60 * 24,
      },
      secret: 'mysecret',
      saveUninitialized: false,
      resave: false,
    }),
  );

I did read from the NestJS documentation that i can setup Redis as a microservice however i really only need Redis for my Express-Session and dont want to set up the redis microservice if i can get this fixed.

I also use Mongoose to connect to my MongoDB which i use for my repositories inside of NestJS. Previously in other projects instead of using Redis i would setup my store in TypeORM using ORMSession if anyone has an alternative of this that works with Mongoose then that would also work.

const sessionRepo = getRepository(TypeORMSession);

...

store: new TypeormStore().connect(sessionRepo),

2

Answers


  1. The error is stated here connect_redis1.default is not a function.

    Instead, you should use import * as connectRedis from 'connect-redis'. I’ve got an example here which looks like this:

    import { Inject, Logger, MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
    import * as RedisStore from 'connect-redis';
    import * as session from 'express-session';
    import { session as passportSession, initialize as passportInitialize } from 'passport';
    import { RedisClient } from 'redis';
    
    import { AppController } from './app.controller';
    import { AppService } from './app.service';
    import { AuthModule } from './auth';
    import { REDIS, RedisModule } from './redis';
    
    @Module({
      imports: [AuthModule, RedisModule],
      providers: [AppService, Logger],
      controllers: [AppController],
    })
    export class AppModule implements NestModule {
      constructor(@Inject(REDIS) private readonly redis: RedisClient) {}
      configure(consumer: MiddlewareConsumer) {
        consumer
          .apply(
            session({
              store: new (RedisStore(session))({ client: this.redis, logErrors: true }),
              saveUninitialized: false,
              secret: 'sup3rs3cr3t',
              resave: false,
              cookie: {
                sameSite: true,
                httpOnly: false,
                maxAge: 60000,
              },
            }),
            passportInitialize(),
            passportSession(),
          )
          .forRoutes('*');
      }
    }
    
    Login or Signup to reply.
  2. you need to install @types/connect-redis

    and do

    import * as _connectRedis from ‘connect-redis’;

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search