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

mongoose failing Next.js build — node_modules/mongoose/dist/browser.umd.js #13390

Closed
1 task done
dangtony98 opened this issue May 10, 2023 · 16 comments
Closed
1 task done
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

Comments

@dangtony98
Copy link

dangtony98 commented May 10, 2023

Prerequisites

  • I have written a descriptive issue title

Mongoose version

7.1.0

Node.js version

18.12.1

MongoDB version

Atlas

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

12.0 — Monterey

Issue

When I run npm run build, I get the following error:

next build

- warn You have enabled experimental feature (serverComponentsExternalPackages) in next.config.js.
- warn Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk.

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/app/api/helpers/auth.ts
./src/app/api/helpers/index.ts


> Build failed because of webpack errors
- info Creating an optimized production build .%                                                  

While I was developing fine with mongoose and Next.js using next dev, I encountered this issue when building the application with next build. It'd be amazing if someone could help me out here.

To reproduce this error, you can simply clone my repository here and start up the application; should take a few seconds to do that.

If you run npm run dev, then the app will startup fine and you can navigate to http://localhost:3000/auth/login.

However, if you run npm run build, then you'll receive the error Failed to compile with the message in the above code block.

@dangtony98 dangtony98 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels May 10, 2023
@vkarpov15
Copy link
Collaborator

Have you tried the suggestion in this comment: #13252 (comment) ?

@dangtony98
Copy link
Author

@vkarpov15 Yes I have! I've added them both to my next.config.js file.

@vkarpov15
Copy link
Collaborator

Are you explicitly importing mongoose/browser?

Also, are you able to share a code sample that demonstrates this issue?

@vkarpov15 vkarpov15 added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label May 12, 2023
@dangtony98
Copy link
Author

dangtony98 commented May 12, 2023

I'm importing mongoose not mongoose/browser.

If you clone the repo I shared and just run npm run build then you should see the error that I listed. I'm sure it's something small — I'll continue debugging myself as well.

GitHub repo: https://github.com/Infisical/infisical-customer-portal

@vkarpov15 vkarpov15 modified the milestones: 7.1.2, 7.1.3 May 13, 2023
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity labels May 13, 2023
@sundaram2021
Copy link

Exaclty same issue

and whenever you try to run the server using npm run dev
and try to post something to the server it results into this error

`- error node_modules\mongoose\lib\types\objectid.js (21:31) @ prototype

  • error Error [TypeError]: Cannot read properties of undefined (reading 'prototype')`

next version - 13.4.1
mongoose - 7.1.1

@dangtony98
Copy link
Author

@sundaram2021 So actually my npm run dev works perfectly fine but not npm run build; I'm hoping to debug this in the next day or two — Will keep you posted.

@sundaram2021
Copy link

hey , @dangtony98 I have resolve the issue .
I think there is some problem with the mongoose version 7.1.1 with next js 13.4.1.

but if you move to mongoose version 7.0.3 then it will work fine with the next 13.4.1 version

hope it helps

@dangtony98
Copy link
Author

@sundaram2021 Unfortunately that didn't work for me but I'm sure it was right.

I think my issue has something more to do with the separation of client and server-side code in Next.js.

Thanks though!

@sundaram2021
Copy link

you can refer my repository https://github.com/sundaram2021/blog-typescript-fullstack

i will be committing all the changes soon

@dangtony98
Copy link
Author

So I finally managed to resolve this issue!

I had the correct base setup for adding mongoose to Next.js but my specific case used multiple mongoose connections, so many examples didn't apply to me. I ended up having to wrap my models with an async function, call the functions to get the models, and then make any queries/mutations.

I'll probably end up writing a blog about this since it was quite frustrating.

@vkarpov15 vkarpov15 reopened this May 23, 2023
@vkarpov15
Copy link
Collaborator

It looks like @dangtony98 's repo is no longer public and I'm unable to repro using @sundaram2021 's repo.

I cloned https://github.com/sundaram2021/blog-typescript-fullstack, changed Mongoose version to 7.1.1, tested with both npm run dev and npm run build + npm start. In both cases, creating a user succeeds:

$ curl -X POST http://localhost:3000/api/register -d '{"username":"foo","email":"bar","password":"baz"}'
{"message":"User created successfully!"}

@vkarpov15 vkarpov15 removed this from the 7.2.2 milestone May 26, 2023
@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels May 26, 2023
@dangtony98
Copy link
Author

Hi @vkarpov15 I believe both @sundaram2021 and I have resolved the issue; we can close it now :)

@sundaram2021
Copy link

I dont't know what is the issue , it does not work with 7.1.1 version of mongoose but it with 7.0.3 ,
anyhow issue is resolved

@kurays015
Copy link

I'm facing this problem right now with nextjs, next auth 5.0.0-beta.13.

@vkarpov15
Copy link
Collaborator

@kurays015 please make sure you've followed the instructions on our Next.js docs. If you're still having trouble, please open a new issue with detailed repro instructions.

@Monukumar777
Copy link

So I finally managed to resolve this issue!

I had the correct base setup for adding mongoose to Next.js but my specific case used multiple mongoose connections, so many examples didn't apply to me. I ended up having to wrap my models with an async function, call the functions to get the models, and then make any queries/mutations.

I'll probably end up writing a blog about this since it was quite frustrating.

can you provide your blog link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Projects
None yet
Development

No branches or pull requests

5 participants