next-auth: Error: no optional dependency [mongodb] defined in peerOptionalDependencies in any package.json
Describe the bug
I keep getting Error: no optional dependency [mongodb] defined in peerOptionalDependencies in any package.json. It’s so weird because I see it most of the time but no always. Sometimes I can sign in correctly and sometimes I get redirected to /api/auth/signin?error=Callback# and see this error in the client: Try signing with a different account. This is the full error log:
[GET] /api/auth/session
07:12:46:47
2020-08-06T05:12:46.563Z 044494dc-cf2a-4d4d-899c-a8209f59eec8 ERROR [next-auth][error][session_error] Error: no optional dependency [mongodb] defined in peerOptionalDependencies in any package.json
at find_package_json_with_name (/var/task/node_modules/require_optional/index.js:56:11)
at require_optional (/var/task/node_modules/require_optional/index.js:69:13)
at Object.<anonymous> (/var/task/node_modules/next-auth/dist/adapters/typeorm/index.js:118:53)
at Generator.next (<anonymous>)
at asyncGeneratorStep (/var/task/node_modules/next-auth/dist/adapters/typeorm/index.js:28:103)
at _next (/var/task/node_modules/next-auth/dist/adapters/typeorm/index.js:30:194)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
https://next-auth.js.org/errors#session_error
To Reproduce
My code is in a private repo but if it’s necessary I can try to create a new project and replicate it. My code is pretty standard. Just followed the docs. I only have 1 provider (Google) and I’ve already tried emptying the 3 collections (accounts, users and sessions)
Documentation feedback Documentation refers to searching through online documentation, code comments and issue history. The example project refers to next-auth-example.
- Found the documentation helpful
- Found documentation but was incomplete
- Could not find relevant documentation
- Found the example project helpful
- Did not find the example project helpful
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 10
- Comments: 29 (6 by maintainers)
Commits related to this issue
- Try and fix weird no optional dependency error for mongodb https://github.com/nextauthjs/next-auth/issues/552#issuecomment-671662283 — committed to hswolff/votey-uppy by hswolff 4 years ago
- add fix to mongodb dependancy error https://github.com/nextauthjs/next-auth/issues/552#issuecomment-671662283 — committed to ListKeeper/ListKeeper by npwxx 4 years ago
- Fix next-auth mongo error reference: https://github.com/nextauthjs/next-auth/issues/552 — committed to dtjv/memowise by dtjv 3 years ago
- FIX Next-Auth: Error: no optional dependency [mongodb] defined - Error: no optional dependency [mongodb] defined in peerOptionalDependencies in any package.json - @see https://github.com/nextauthjs/n... — committed to devdev-dev/eztimate by deleted user 3 years ago
- Fix - next-auth / require_optional pkg Error https://github.com/nextauthjs/next-auth/issues/552#issuecomment-671662283 — committed to luizwhite/iliketomoveit-nextjs-nlw04 by luizwhite 3 years ago
For context, the error is from this line in the
require_optionalpackage: https://github.com/christkv/require_optional/blob/master/index.js#L61NextAuth.js uses this package to load MongoDB when a MongoDB database is specified, so it can load the ObjectID type.
The
require_optionalpackage is used by MongoDB driver itself to the same thing - which is why it replaced dynamic imports (which 2.x used) because quite a few folks had old / weird config setups that couldn’t handle dynamic import statements yet.Given three people who reported this have now cited a path of
/var/task/node_modules/I’m inclined to thing it’s something that is going on with the build process and sounds likepackage.jsonis being removed as part of the build process (this will definitely break some libraries).It’s hard to say for sure without code to replicate this, but we might be at something of an impasse between people who can’t use
require_optionalbecause of their build setup and people who can’t use dynamic imports because for the same reason.(I can think of a way to address it for both groups, but it would be a breaking change and one I would rather avoid.)
It would be interesting to know if
/var/task/node_modules/next-authcontains apackage.jsonfile and what it contains, or if that is getting stripped for some reason.Adding
mongodbtopeerOptionalDependenciesin your own appspackage.jsonmight help, as long as it is bundled with the application:Getting a new error now 😢 Still not touching any back end code. Same error in the logs (
Error: no optional dependency [mongodb] defined in peerOptionalDependencies in any package.json)npm i mongodb&3.5.9 --save-optionalit has solved my issuemy error was
[next-auth][error][session_error] https://next-auth.js.org/errors#session_error Error: optional dependency [mongodb] found but version [4.0.1] did not satisfy constraint [^3.5.9]Since I added
mongotopeerOptionalDependenciesI’m not seeing the error anymore. Thanks!I cannot replicate this with the example project and am not seeing this issue on other projects.
If you are experiencing a problem please provide an example with steps to replicate.
Just to add some search engine optimization to this issue, this error happens when deploying next-auth on Vercel and the solution @iaincollins posted seems to fix it!
In v4, you will explicitly import adapters, and we won’t try to import
typeormby default. So hopefully this problem will be resolved then. We aim for a release some time this summer 🤞I’m getting this seemingly random as well. It starts happening, then nothing is working because of it for a few minutes. Then it disappears again, to come back a few hours later.
@matiastucci I’m using mongoose and I created all the models inside one folder so I just import any model inside the API
I had this issue today twice and will check if I can create an repo to reproduce it.
I think I fixed it, just need delete package.lock and node modules then run
npm iagain