URGENT - Help with build error
Answered
Japanese flying squid posted this in #help-forum
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
▲ 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
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
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 env62 Replies
You are calling
auth
in middleware which calls mongoose and mongoose can't run in edge env as middleware runs in edge envAnswer
Mongoose is not available on edge - maybe you are using it inside your middleware?
Japanese flying squidOP
Thanks. Any other solution to protect admin pages?
check the user on the page
Japanese flying squidOP
No other solutions? 😦
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
Japanese flying squidOP
But when user sign in using google:
How can I also create account in Mongo
Also I was using it before this NextAuth 5.0 beta
In Next 14
Japanese flying squidOP
@James4u (Tag me if needed) How can I create mongoose model for google user after sign in?
is this your api route, right?
then you should be able to use Mongoose
I only meant middleware (edge runtime)
Japanese flying squidOP
No no, I am using it in SignIn callback inside NextAuth
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
Japanese flying squidOP
Error is because
Middleware using
auth
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
Japanese flying squidOP
wait... middleware is not running on server side?
yeah, on the server
Japanese flying squidOP
so why can't I use mongoose library there?
sorry, it runs on the edge
Japanese flying squidOP
Aha so probably I should be able to send API request
Wait, let me try sir
Japanese flying squidOP
No, I can't send API request
Btw, this error is only in build
Japanese flying squidOP
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
you can't do fetch here?
btw
i suggest u to migrate to v5
also what's your middleware like? @Japanese flying squid
Japanese flying squidOP
I am using v5
I am very confused
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)
[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)
no no, show me your code instead?
Japanese flying squidOP
well, you can't use relative path - it's not running on the client - api path needs to be absolute one
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?
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