The mistake occurred due to the inclusion of 'mongoose/dist/browser.umd.js' in the file './config/mongodb/index.ts'

I successfully developed my custom application using next.js version 14 and mongoose version ^8.4.4, which functions flawlessly on my local machine.

However, when attempting to deploy the application on Vercel, I encountered the following error in the console:

Failed to compile.
./config/mongodb/index.ts
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 issue arose from importing 'mongoose/dist/browser.umd.js' in './config/mongodb/index.ts' and led to a webpack build failure with exit code 1.
Error: Command "pnpm run build" exited with 1

Below is a snippet of my code that is causing this issue:

import mongoose from 'mongoose';

interface Cached {
  conn: typeof mongoose | null;
  promise: Promise<typeof mongoose> | null;
}

const cached: Cached = {
  conn: null,
  promise: null,
};

export const connect = async () => {
  try {
    // Code for connecting to MongoDB...
  } catch (error) {
    throw error;
  }
};

export const disconnect = async () => {
  // Code for disconnecting from MongoDB...
};

While the project works fine locally, the deployment on Vercel seems to be throwing this specific error. Any insights on what may be causing this?

https://i.sstatic.net/Qm2SGrnZ.png

Answer №1

Currently dealing with a unique type of issue.

The problem lies in the middleware - it is set to run on "edge" runtime, which restricts certain functions that are allowed on "nodejs" runtime.

Here's a workaround that worked for me:

export const config = {
    matcher: "/:path*",
    runtime: "nodejs",
    unstable_allowDynamic: [
        // allows a single file
        "/src/db/lib/dbConnect.js",
        // use a glob to allow anything in the function-bind 3rd party module
        "/node_modules/mongoose/dist/**",
    ],
};

Add this section (customize the path to your own library destination) at the end of your middleware file

also refer to this part of the documentation

my custom middleware implementation

successful build outcome screenshot

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular 2 - Beginner's guide to updating the header when navigating to a new route or page

SOLVED I am faced with a challenge involving multiple components in Angular2. Specifically, I have the following components: home component, default header, detail component, and detail header component. Each header has a unique layout. At the Home pa ...

Tips for optimizing compilation of TypeScript during the packaging process using Electron Forge

Upon opening the app, there is a momentary delay with a blank screen before it fully loads. I have utilized electron-forge's react-typescript template. While I am able to successfully create a dmg or deb file, I have observed that when running the p ...

Generate detailed documentation for the functional tests conducted by Intern 4 with automated tools

I need to automatically generate documentation for my Intern 4 functional tests. I attempted using typedoc, which worked well when parsing my object page functions. However, it failed when working with functional test suites like the one below: /** * Thi ...

Testing TypeScript data types through unit tests

In one of my modules, I have the following export: export class Action1 implements Action {} export class Action2 implements Action {} export type ActionsUnion = | Action1 | Action2; I'm currently exploring the best approach to testing Actions ...

Trouble with implementing Mongoose's updateMany() method within a nested model

I have a mongoose model named Chatroom that is structured like this: [ { _id: 60cd9816b9326c5cb40ca24c, chat_history: [ { read_flag: 0, receiver_id: "60cc83b8b0ebb52f43278b1b" }, { read_flag: 0, ...

Enhance your TypeScript React development with NeoVim

As a newcomer to vim, I decided to test my configuration with react and typescript. To do this, I created a simple demo app using the command npx create-react-app demo --template typescript. Next, I opened the directory in neovim by running nvim .. However ...

Looking to display a combination of document data and referenced document data from mongoose using react.js and redux

This project involves MERN stack with Redux and mongoose. It consists of two schemas - "User" and "Reader". A user creates readers, and the challenge is to render each reader along with the respective user on the client side. While the backend works fine w ...

Utilizing conditional binding with ngModel in Angular 5

In my Angular 5 project, I am facing some issues. I have two models called Person and Employee where Employee inherits from Person and has its own attributes. In the HTML file of my component, I created a form with several input fields: <input type="te ...

finding mongo indexes that apply multiple filters and sorting

Imagine having this schema in a collection named Test: { "uid": "5e0e6a804888946fa61a1976", "name": "abc", "bookName":"xyz", "rating": 5, "category":"aa", "isbn& ...

Achieving top document ranking based on points: A guide to effective querying

When storing a document in MongoDB, I need to retrieve data in a way that the userId with the highest total sum of points and rank is returned. As someone new to MongoDB, I would appreciate guidance on how to achieve this using a query. The following data ...

What is preventing TypeScript from automatically inferring the type of an argument in a generic function that utilizes `keyof`?

What causes the error in this code snippet? type MyType = { a: string, b: string } function cantInfer<In, Out>(fn: (i: In) => Out, i: In) { } function myFunction<K extends keyof MyType>(key: K): string { return ''; } ...

issue with angular and typescript

I'm currently working on developing an Angular 2 application that incorporates touch gestures using hammerjs. My goal is to merge the quickstarter application from: Angular 2 with the hammerjs application from: Hammerjs sample. However, I keep encou ...

What is the best way for me to use a ternary operator within this code snippet?

I'm in the process of implementing a ternary operator into this snippet of code, with the intention of adding another component if the condition is false. This method is unfamiliar to me, as I've never utilized a ternary operator within blocks of ...

Tips for passing parameters from an anchor click event in TypeScript

Is it possible to send parameters to a click event from an anchor element, or should we not pass params at all? Here is the function I am using: const slideShow = (e: React.MouseEvent<HTMLAnchorElement> | undefined): void => { console.lo ...

Inject Angular 2 component into designated space

I am working on a website that requires a settings dialog to be loaded in a designated area upon clicking a button. The settings dialog is a component that retrieves data from REST endpoints. I am hesitant to simply insert the component and hide it as I ...

The module for the class could not be identified during the ng build process when using the --

Encountering an error when running: ng build --prod However, ng build works without any issues. Despite searching for solutions on Stack Overflow, none of them resolved the problem. Error: ng build --prod Cannot determine the module for class X! ...

Tips for developing a personalized form validator for validating JSON data exclusively in Angular

Within my Angular application, there exists a reactive form that has a single control known as configJson, which is visually represented by a <textarea> element in the DOM. The primary goal is to validate this specific form control to ensure that it ...

What programming language is the best choice for Angular 2 development?

As someone who is new to Angular 2, I've discovered that developers have the option to use TypeScript, ES6, and ES5 for their development needs. I understand that TypeScript is considered the superset of ES6 and ES5. Given the stark differences in sy ...

What is the best approach for submitting a form with data through a POST request in an Ionic application?

I am facing an issue while trying to make a POST request in Ionic for submitting a form with an array of data. Surprisingly, it works perfectly fine when I test it on POSTMAN. https://i.sstatic.net/t8sEG.jpg Although I attempted to use this form, it did ...

Discovering the most recent 10 date elements in a JSON object within a React application

I have an array of objects containing a date element. My goal is to identify the 10 most recent dates from this element and display them in a table format. When I attempt to render these dates using a mapping technique that targets each data with data.date ...