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
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:you need to install @types/connect-redis
and do
import * as _connectRedis from ‘connect-redis’;