NextAuth.js Authentication with MongoDB
Learn how to set up NextAuth.js for user authentication in your Next.js 14 application with MongoDB. This step-by-step guide covers integrating email/password logins, configuring MongoDB, and implementing secure authentication methods to enhance your Next.js project.
Read more on Developer Center
Author: Ahmed Bouchefra
this code is not working on production mode. it’s showing error
The error was caused by importing 'mongoose/dist/browser.umd.js
Hi @Akash_Mishra1, I’m sorry you are having issues. Do you have a repo that you can share so that I can try to reproduce the error? Thanks!
None of the mongoose examples with NextJS 14 are working on prod, just take your example and do “npm run build” , and you will get the same error that I am getting:
./lib/db.ts
Dynamic Code Evaluation (e. g. ‘eval’, ‘new Function’, ‘WebAssembly.compile’) not allowed in Edge Runtime
Learn More: Dynamic code evaluation is not available in Middleware | Next.js
The error was caused by importing ‘mongoose/dist/browser.umd.js’ in ‘./lib/db.ts’.
you can write me at: dvirben123@gmail.com , and I will give you access to my repo in order to see this error.
@Dvir_Ben_Ishay Hello, I am also facinf exact issue from past 7 days. Failed to figure out this issue. Have you resolved this issue?. Please inform or you can write me at: swapnilkotkar793@gmail.com
Unfortunately, no.
But I found a good place to start the connection to mongo once the app is started, create a file: instrumentation.ts (root folder). and put the connection function there
Thak you for replying. Actually, I already did that but still it failed to build nextjs app on vercel. I am using “use server” everywhere almost. Integrated middleware.ts along with auth.js(v5). This is actual build error on my vercel deployment,
Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime
Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation
The error was caused by importing 'mongoose/dist/browser.umd.js' in './lib/database/index.ts'.`
If you know this issue, please let me know. I am unable to figure this out from past 30 days. Now, If i will not able to figure this out in next 2 days then I have decided to move to ReactJS over NextJS.
I think I found the issue on my side, I have replaced the server call with an API call in the auth.config.ts - authorize function (next-auth-v5 latest beta), and then “npm run build” passed and vercel deployment passed as well.
yeah, You are right. I will replace all my logic with APIs now