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

Most upvoted comments

For context, the error is from this line in the require_optional package: https://github.com/christkv/require_optional/blob/master/index.js#L61

NextAuth.js uses this package to load MongoDB when a MongoDB database is specified, so it can load the ObjectID type.

The require_optional package 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 like package.json is 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_optional because 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-auth contains a package.json file and what it contains, or if that is getting stripped for some reason.

Adding mongodb to peerOptionalDependencies in your own apps package.json might help, as long as it is bundled with the application:

"peerOptionalDependencies": {
  "mongodb": "^3.6.0"
},

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)

Screen Shot 2020-08-09 at 4 56 24 AM

npm i mongodb&3.5.9 --save-optional it has solved my issue

my 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 mongo to peerOptionalDependencies I’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 typeorm by 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 i again