Next.js Discord

Discord Forum

URGENT - Help with build error

Answered
Japanese flying squid posted this in #help-forum
Open in Discord
Avatar
Japanese flying squidOP
Repository: https://github.com/LukaKoridze13/fishy-fishy-fishy

Error:

PS C:\Users\lukak\OneDrive\Desktop\repositories\fishy-fishy-fishy> npm run build

fish-store@0.1.0 build
next build

▲ Next.js 15.0.2
- Environments: .env.local

Creating an optimized production build ...
Failed to compile.

./node_modules\mongoose\dist\browser.umd.js
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

Import trace for requested module:
./node_modules\mongoose\dist\browser.umd.js
./src\mongodb\models\RoleModel.ts
./src\next_auth\auth.ts
./src\middleware.ts


Build failed because of webpack errors
Image
Answered by Yi Lon Ma
You are calling auth in middleware which calls mongoose and mongoose can't run in edge env as middleware runs in edge env
View full answer

62 Replies

Avatar
You are calling auth in middleware which calls mongoose and mongoose can't run in edge env as middleware runs in edge env
Answer
Avatar
Mongoose is not available on edge - maybe you are using it inside your middleware?
Avatar
Japanese flying squidOP
Thanks. Any other solution to protect admin pages?
Avatar
check the user on the page
Avatar
Japanese flying squidOP
No other solutions? 😦
Avatar
As long as Mongoose is not available on the edge, where you can check auth is on your page, no?
Next.js team is working hard on Node.js support on edge - but not sure when it will be supported
Avatar
Japanese flying squidOP
But when user sign in using google:
Image
How can I also create account in Mongo
Also I was using it before this NextAuth 5.0 beta
In Next 14
Avatar
Japanese flying squidOP
@James4u (Tag me if needed) How can I create mongoose model for google user after sign in?
Avatar
is this your api route, right?
then you should be able to use Mongoose
I only meant middleware (edge runtime)
Avatar
Japanese flying squidOP
No no, I am using it in SignIn callback inside NextAuth
Avatar
yeah, so it must be an api route, right? then you are able to use Mongoose
error must be from your middleware
Import trace for requested module:
  ./node_modules\mongoose\dist\browser.umd.js
  ./src\mongodb\models\RoleModel.ts
  ./src\next_auth\auth.ts
  ./src\middleware.ts
see the error trace
Avatar
Japanese flying squidOP
Error is because
Middleware using
auth
Avatar
aha you mean Middleware is using auth? okay then
let's try this, move all DB operations into a seperated api route
and hit that api route in your /auth
hmm am I misleading? @Yi Lon Ma
Avatar
Japanese flying squidOP
wait... middleware is not running on server side?
Avatar
yeah, on the server
Avatar
Japanese flying squidOP
so why can't I use mongoose library there?
Avatar
sorry, it runs on the edge
Avatar
Japanese flying squidOP
Aha so probably I should be able to send API request
Wait, let me try sir
Avatar
Japanese flying squidOP
No, I can't send API request
Btw, this error is only in build
Avatar
where?
fetch is supported on the edge
which one?
Avatar
Japanese flying squidOP
Image
Avatar
ok?
what am i suppose to do with this info
if u have issues with nextauth you should tell me which ones, the one issue you have is because of your usage of whatever node module
in middleware
which james told u is not supported
Avatar
you can't do fetch here?
Avatar
btw
i suggest u to migrate to v5
Avatar
also what's your middleware like? @Japanese flying squid
Avatar
Japanese flying squidOP
I am using v5
I am very confused
Image
Avatar
why?
Avatar
Japanese flying squidOP
[auth][error] AccessDenied: Read more at https://errors.authjs.dev#accessdenied
[auth][cause]: TypeError: Invalid URL
at new URL (node:internal/url:814:29)
at dispatchHttpRequest (webpack-internal:///(rsc)/./node_modules/axios/lib/adapters/http.js:262:20)
Avatar
no no, show me your code instead?
Avatar
Japanese flying squidOP
Image
Image
Avatar
well, you can't use relative path - it's not running on the client - api path needs to be absolute one
Avatar
Japanese flying squidOP
It worked, thank you so much
Guys one more question
Is it good aproach to stay on old versions in new project for example Next 14 and NextAuth 4, I've used it, it has everything I need, so how good aprach is to use them insetad new ones?
Avatar
well, not that sure about NextAuth but I would recommend stay on Next.js v14 for now if it's a production website
if you just want to try Next.js 15, it's okay but if it's a product, I would recommend v14
there aren't many breaking changes in v15 migration so you would be able to migrate easily when v15 is stable and production ready