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?