session
session copied to clipboard
connect-redis no longer compatible with @fastify/session
Prerequisites
- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the bug has not already been reported
Fastify version
4.24.3
Plugin version
10.6.0
Node.js version
18.17.0
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
14.0
Description
Trying to make this plugin work with the latest version of connect-redis
will fail since express-session
is a peer dependency of it since version 7.0.0
. RedisStore is extending Store
from express-session
and also using the SessionData
type from it as you can see here: https://github.com/tj/connect-redis/blob/master/index.ts
This leads to the following issue when trying to run a project without express-session
installed:
Error: Cannot find module 'express-session'
This can be fixed by downgrading the connect-redis
version to v6.1.3
Steps to Reproduce
- Set up a project with Fastify, @fastify/session and
connect-redis
- Run the project
- You should get the error message mentioned above
Expected Behavior
No response
Everything is functioning properly. I simply duplicated the contents of examples/redis.js into a new file named /server.js and included the following package.json:
{
"name": "fastify-session-redis",
"version": "1.0.0",
"main": "server.js",
"scripts": {
"start": "node ."
},
"dependencies": {
"@fastify/cookie": "^9.2.0",
"@fastify/session": "^10.6.0",
"connect-redis": "^7.1.0",
"fastify": "^4.24.3",
"ioredis": "^5.3.2"
}
}
Everything is functioning properly. I simply duplicated the contents of examples/redis.js into a new file named /server.js and included the following package.json:
{ "name": "fastify-session-redis", "version": "1.0.0", "main": "server.js", "scripts": { "start": "node ." }, "dependencies": { "@fastify/cookie": "^9.2.0", "@fastify/session": "^10.6.0", "connect-redis": "^7.1.0", "fastify": "^4.24.3", "ioredis": "^5.3.2" } }
Hm, that's weird, it doesn't make sense since the library is indeed requesting express-session
as a peer dependency. The only possibility I see for not getting the error is having express-session
installed globally in your machine.
Before I was trying over NestJs so I tried again following your steps and I'm still getting the same issue as you can see here
For me it makes total sense since I didn't install the library and connect-redis
wants to make use of it.
In case someone is experiencing the same issue, I've forked the original connect-redis
library and made the necessary changes for it to work with @fastify/session
. Anyone is more than welcome to contribute to it.
https://www.npmjs.com/package/fastify-session-redis-store
I think it would be likely worthwhile to contribute RedisStore
directly to this module. Would you like to send a PR?
At a glance, https://github.com/mgcrea/fastify-session-redis-store/blob/master/src/RedisStore.ts looks like it is better implemented.
Could not get @fastify/session to work despite trying for ours. Things like destroying session do not work. TypeScript types are confusing. It is possible that this was an issue with the fastify-session-redis-store not working.
Switched to https://github.com/mgcrea/fastify-session and it worked from first try.
@mcollina maybe deprecate in favor of https://github.com/mgcrea/fastify-session? Seems like a nice ecosystem that's well documented and tested.