ERROR [Better Auth]: Failed to create user BSONVersionError: Unsupported BSON version, bson types must be from bson 6.x.x
Adapter type
@auth/fauna-adapter, @auth/mongodb-adapter
Environment
Env Info:
System:
OS: macOS 26.0.1
CPU: (14) arm64 Apple M4 Max
Memory: 382.17 MB / 36.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 23.6.0 - /Users/phoenix/.nvm/versions/node/v23.6.0/bin/node
npm: 11.6.2 - /Users/phoenix/.nvm/versions/node/v23.6.0/bin/npm
pnpm: 10.20.0 - /Users/phoenix/.nvm/versions/node/v23.6.0/bin/pnpm
bun: 1.3.2 - /Users/phoenix/.bun/bin/bun
Browsers:
Chrome: 142.0.7444.135
Chrome Canary: 144.0.7524.0
Firefox: 142.0.1
Safari: 26.0.1
Project Info:
macOs 26.0.1
[create better-t-stack](https://github.com/AmanVarshney01/create-better-t-stack)
better-auth 1.3.28
Mongodb 7.0.0
Mongoose 8.19.3
next 16.0.0
typescript 5.9.3
Reproduction URL
https://github.com/skipzero/chat-app/tree/user-list
Describe the issue
trying to sign up or login with existing account results in the bson error
on sign up...
server log...
same log on signing in if there's already an account created.
How to reproduce
I was getting warnings about mongo being an external package and conflicting version so I got mongo and mongoose to both have matching versions across the project and not throw errors/warnings. Now I get the bson issue.
Clone the repo/branch above and install regularly (npm i, pnpm..., etc). Navigate to the signup/sign in pages and enter requested info and observe the server console throw the error and nothing is added to the mongoDB.
I'm using a local copy of mongo at localhost:3001/27017. Environmental variables are...
DATABASE_URL=mongodb://localhost:27017/<DB_NAME>
BETTER_AUTH_SECRET=<AUTH_SECRET>
BETTER_AUTH_URL=http://localhost:3001
CORS_ORIGIN=http://localhost:3001
Expected behavior
I would expect a new user added if I were signing up and a new session initialised if I were signing in.
I ran into this recently using better-auth but I believe it is due to the fact that Mongoose 8.x actually requires mongodb 6.x and not 7.x. You will likely find your app has issues in general trying to use mongodb 7 with Mongoose 8 particularly related to ObjectId generation.
I decided to resolve this by upgrading to the Mongoose 9.x release candidate which uses mongodb 7 which was just posted by their dev team. My app is not in production yet and I would rather run into any issues sooner rather than later anyway.
There are some breaking changes to review in the release notes below. I reviewed them all for my app and only had minor fixes (removing usages of next() in some pre hooks).
Release notes: https://github.com/Automattic/mongoose/releases/tag/9.0.0-rc0
Ran the update with npm install [email protected] and it seems to be fine now.
BetterAuth does not currently officially support Mongoose. But looks like you found out the solution