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

Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime #13430

Closed
1 task done
Aralhi opened this issue May 23, 2023 · 6 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@Aralhi
Copy link

Aralhi commented May 23, 2023

Prerequisites

  • I have written a descriptive issue title

Mongoose version

7.2.0

Node.js version

18.x

MongoDB version

5/x

Operating system

macOS

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

13

Issue

when i use mongoose in nextjs edge runtime meet this error

./node_modules/_mongoose@7.2.0@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@7.2.0@mongoose/dist/browser.umd.js
./lib/dbConnect.ts
./lib/completion.ts

does mongoose support nextjs edge runtime?

api/get.ts

const handler = async (req: Request, res: Response) => {
   await dbConnect
})

dbconnect.ts

import mongoose from 'mongoose'

const MONGODB_URI = process.env.MONGODB_URI || ''

if (!MONGODB_URI) {
  throw new Error(
    'Please define the MONGODB_URI environment variable inside .env.local'
  )
}

let cached = global.mongoose

if (!cached) {
  cached = global.mongoose = { conn: null, promise: null }
}

async function dbConnect() {
  if (cached.conn) {
    return cached.conn
  }

  if (!cached.promise) {
    const opts = {
      bufferCommands: false,
    }

    cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => {
      return mongoose
    })
  }

  try {
    cached.conn = await cached.promise
  } catch (e) {
    cached.promise = null
    throw e
  }

  return cached.conn
}

export default dbConnect

HOW can i resolve it.

@Aralhi Aralhi added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels May 23, 2023
@Aralhi
Copy link
Author

Aralhi commented May 23, 2023

and i try to use unstable_allowDynamic and it does not work.

export const config = {
  runtime: "edge",
  unstable_allowDynamic: [
    '/node_modules/mongoose/dist/browser.umd.js'
  ]
};

@vkarpov15
Copy link
Collaborator

We've never tested with nextjs edge runtime, so we're not entirely sure. However, dist/browser.umd.js is Mongoose's browser build, so unless you're using Mongoose on the frontend that code is unused.

@vkarpov15 vkarpov15 added this to the 7.2.3 milestone May 23, 2023
@Aralhi
Copy link
Author

Aralhi commented May 24, 2023

We've never tested with nextjs edge runtime, so we're not entirely sure. However, dist/browser.umd.js is Mongoose's browser build, so unless you're using Mongoose on the frontend that code is unused.

thanks for your reply, i will try to ask nextjs for help

@Aralhi
Copy link
Author

Aralhi commented May 24, 2023

We've never tested with nextjs edge runtime, so we're not entirely sure. However, dist/browser.umd.js is Mongoose's browser build, so unless you're using Mongoose on the frontend that code is unused.

one question.why nextjs will build borwser.umd.js. I'm importing it the wrong way?
import { Schema, model, models } from 'mongoose'

@vkarpov15
Copy link
Collaborator

I don't know enough about how nextjs' compiler works to answer. Maybe it just compiles all files, or maybe Next edge runtime imports browser build? But you shouldn't be running browser.umd.js unless you're importing Mongoose on frontend.

@vkarpov15 vkarpov15 added needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels May 24, 2023
@vkarpov15
Copy link
Collaborator

I took a look and you currently can't use Mongoose to connect to MongoDB in NextJS edge runtime because NextJS edge runtime doesn't allow using native node APIs, including net, which is what the MongoDB Node driver uses to connect to MongoDB.

Furthermore, it looks like whatever the build process NextJS edge runtime uses pulls the "browser" version of every npm package based on the browser property in package.json, so if you import Mongoose in Edge runtime you'll get the browser version of Mongoose. The browser version of Mongoose currently only supports hydrating documents, doesn't support connecting to MongoDB.

We will add docs for this.

@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels May 26, 2023
@vkarpov15 vkarpov15 modified the milestones: 7.2.3, 7.2.2 May 26, 2023
vkarpov15 added a commit that referenced this issue May 26, 2023
docs: add nextjs page with link to next starter app and couple FAQs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests

2 participants