Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: no optional dependency [mongodb] defined in peerOptionalDependencies in any package.json #552

Closed
2 of 5 tasks
matiastucci opened this issue Aug 6, 2020 · 29 comments
Labels
hacktoberfest-help-needed The maintainer needs help due to time constraint/missing knowledge incomplete Insufficient reproduction. Without more info, we won't take further actions/provide help.

Comments

@matiastucci
Copy link

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
@matiastucci matiastucci added the bug Something isn't working label Aug 6, 2020
@iaincollins
Copy link
Member

Hi there! Hmm that's odd. It would be appreciate if you could share a link to a repo to replicate this issue.

I wonder if it's related to a custom webpack or babel config that is maybe doing something odd (or possibly a typo in an import statement somewhere?)

@iaincollins iaincollins added the hacktoberfest-help-needed The maintainer needs help due to time constraint/missing knowledge label Aug 6, 2020
@matiastucci
Copy link
Author

matiastucci commented Aug 6, 2020

thanks for answering. Can I add you to the repo?

EDIT: at the time posting the link I tried it and asked a friend to try it too and we both were seeing the error. I just tried it again and don't see it 🤔

@emetelyov
Copy link

emetelyov commented Aug 7, 2020

I have the same issue:

2020-08-07T22:07:05.075Z	5079c22a-9a50-43bb-a19b-ea772c6ba6fd	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

@kevarifin14
Copy link

kevarifin14 commented Aug 8, 2020

I'm also experiencing the same issue:

2020-08-08T17:33:56.202Z	2d4d9902-78ef-4ee3-8b6f-5d0f87f02a46	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

@matiastucci
Copy link
Author

matiastucci commented Aug 8, 2020

FYI, I didn't see this issue for 2 days and now I'm seeing it again. Didn't change any back end code though. But I noticed that 1 minute before seeing this error I got this one:

MongooseServerSelectionError: connection <monitor> to 127.0.0.1:27017 closed
    at NativeConnection.Connection.openUri (/Users/mati/Development/annotations/node_modules/mongoose/lib/connection.js:827:32)
    at Mongoose.connect (/Users/mati/Development/annotations/node_modules/mongoose/lib/index.js:335:15)
    at dbConnect (webpack-internal:///./utils/dbConnect.js:12:68)
    at handler (webpack-internal:///./pages/api/annotations/index.js:28:73)
    at runMicrotasks (<anonymous>)

message: 'connection <monitor> to 127.0.0.1:27017 closed',
  reason: TopologyDescription {
    type: 'Single',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map { 'localhost:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  }
}

not sure if it's related

@matiastucci
Copy link
Author

matiastucci commented Aug 9, 2020

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

@nyedidikeke
Copy link
Contributor

Please share link to public repository or steps to replicate including versions (Nodejs, npm, etc.).

@IRediTOTO
Copy link

I have this error too

@IRediTOTO
Copy link

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

I got this, let's see in the Warning https://next-auth.js.org/providers/google

@IRediTOTO
Copy link

I think I fixed it, just need delete package.lock and node modules then run npm i again

@iaincollins iaincollins added incomplete Insufficient reproduction. Without more info, we won't take further actions/provide help. and removed bug Something isn't working labels Aug 9, 2020
@iaincollins
Copy link
Member

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.

@alsemany
Copy link

alsemany commented Aug 10, 2020

I faced this issue also even if I removed mongodb and install it again the error will appears again
the solution which work for me as error hint appears

npm uninstall mongodb

to remove the mongodb
then

npm i mongodb --save-optional

it will appears on the package.json like that

{
"optionalDependencies": {
    "mongodb": "^3.6.0"
  }
}

@matiastucci
Copy link
Author

matiastucci commented Aug 10, 2020

FYI, I've removed useUnifiedTopology: true from my mongo connect options and so far so good.

@alsemany
Copy link

unfortunately this works for some hours and the error back again
so this Not Working any more :(

{
"optionalDependencies": {
    "mongodb": "^3.6.0"
  }
}

with or without useUnifiedTopology: true still the problem show for me
I will try to downgrade mongodb and see what will happen

@matiastucci
Copy link
Author

Yeah, I just got the error again after doing the optionalDependencies and useUnifiedTopology change 😢

I know it's not possible to get an answer without posting a project to reproduce it. I'm just posting this here for the rest of us having the same issue.

BTW, @alsemany how are you connecting to mongo?

I'm using the dbConnect example:

import mongoose from 'mongoose'

const connection = {}

async function dbConnect() {
  if (connection.isConnected) {
    return
  }

  const db = await mongoose.connect(process.env.MONGODB_URI, {
    useNewUrlParser: true,
    // useUnifiedTopology: true,
    useFindAndModify: false,
  })

  connection.isConnected = db.connections[0].readyState
}

export default dbConnect

@iaincollins
Copy link
Member

iaincollins commented Aug 11, 2020

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"
},

@alsemany
Copy link

@matiastucci I'm using mongoose and I created all the models inside one folder so I just import any model inside the API

@matiastucci
Copy link
Author

@iaincollins thanks for looking into this. I added mongo to peerOptionalDependencies. Hope that solves it 🤞

@matiastucci
Copy link
Author

Since I added mongo to peerOptionalDependencies I'm not seeing the error anymore. Thanks!

@Zertz
Copy link

Zertz commented Oct 28, 2020

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!

@rajat1saxena
Copy link

My app is deployed on Vercel and I am getting the same error.

[GET] /api/auth/callback/google?state=4bdddef388295676496ad450cd70768b8e80fffb9e673f677854421050270146&code=4%2F0AY0e-g6v49ZSnhqkBJk0PqpY51e5ohcNqAhTEQNzjX86UvWmWwfRtAPsM78NYGB8fxjFOA&scope=email+profile+openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&authuser=0&prompt=none
15:36:32:68
2020-11-26T10:06:32.978Z	70fb4173-7ed1-4838-b4ae-6abc572134ee	ERROR	[next-auth][error][oauth_callback_handler_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#oauth_callback_handler_error

@mnlfischer
Copy link

I had this issue today twice and will check if I can create an repo to reproduce it.

@tx46
Copy link

tx46 commented Dec 20, 2020

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.

@WorkStarostenko
Copy link

I have added peerDependencies and that fixed problem. You're welcome :)

dtjv added a commit to dtjv/memowise that referenced this issue Feb 18, 2021
devdev-dev pushed a commit to devdev-dev/eztimate that referenced this issue Mar 26, 2021
- Error: no optional dependency [mongodb] defined in peerOptionalDependencies in any package.json
- @see nextauthjs/next-auth#552 (comment)
luizwhite added a commit to luizwhite/iliketomoveit-nextjs-nlw04 that referenced this issue Mar 28, 2021
@huksley
Copy link

huksley commented Apr 14, 2021

This is still happening, for example if you switch to using Serverless (AWS) and experimental-serverless-trace in NextJS to disable bundling required modules with your app.

@balazsorban44
Copy link
Member

we will see if #1682 helps with this

@ekasprzyk
Copy link

This is also happening for me. The weird thing is that I've copied the config from two other very similar applications and they work fine. Mine, not so much.

Tried adding peer dependency as mentioned above and no joy.
https://stackoverflow.com/questions/64739543/modulenotfounderror-module-not-found-error-cant-resolve-dns-in-node-modul
adds some interesting information about it. I mean, I'm not using mongodb in my getServerSideProps, so yeah, that would trigger the error, but I'm also not importing typeorm with mongodb, that's next-auth...

@balazsorban44
Copy link
Member

balazsorban44 commented Jun 29, 2021

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 🤞

@coderkhalide
Copy link

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest-help-needed The maintainer needs help due to time constraint/missing knowledge incomplete Insufficient reproduction. Without more info, we won't take further actions/provide help.
Projects
None yet
Development

No branches or pull requests