-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
TypeError: mongoose__WEBPACK_IMPORTED_MODULE_0___default(...).connect is not a function #14271
Comments
What does your Webpack config look like? Are you using Next.js? We'll need a more complete repro to figure this out, because this is a Webpack error and your code looks like it is from Next.js' samples repo. |
I'm using NextJS 14 trying to connect to MongoDB via mongoose below is the connection code import mongoose from "mongoose";
const MONGODB_URI = process.env.NEXT_PUBLIC_MONGODB_URI;
if (!MONGODB_URI) {
throw new Error(
"Please define the MONGODB_URI environment variable inside .env"
);
}
let cached = (global as any).mongoose || { conn: null, promise: null };
export const connectToDatabase = async () => {
if (cached.conn) return cached.conn;
if (!cached.promise) {
const opts = {
bufferCommands: false,
dbName: "festifusion",
};
cached.promise = mongoose.connect(MONGODB_URI, opts).then((mongoose) => {
return mongoose;
});
}
cached.conn = await cached.promise;
return cached.conn;
}; and the /** @type {import('next').NextConfig} */
const nextConfig = {
webpack: (config) => {
return config;
},
images: {
domains: ["utfs.io"],
remotePatterns: [
{
protocol: "https",
hostname: "utfs.io",
port: "",
},
],
},
};
module.exports = nextConfig; if I use webpack: (config) => {
config.experiments = {
topLevelAwait: true
};
return config;
}, then it takes indefinite time to compile the code. I also took a reference from Mongoose Documentation on using with NextJs but still the issue is there I've attached log file for reference purpose. Let me know if you require more details. |
Did you add the following to your Next.js config?
|
Yes by adding this also the issue is not solved the whole |
Any update here? I am also stuck with same issue. I have added
Help would be great! |
We're unable to repro, this GitHub repo demonstrates that Mongoose works fine with Next.js 14. Please fork and modify this repo to demonstrate the issue you're seeing. |
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days |
in my case, because mongoose does not currently support Next Edge Runtime, so just remove line |
I now works by adding
just cleared the cache and deleted |
Hey @Anonymous7code I'm currently getting the same issue with connecting to Mongo, I added the config and it still doesn't work. Could you also show what part of the code you've changed? |
you don't even have to do above, it is totally fine with mongoose and Next 14 , this error is happening just because they are forgetting to mark their server action component with "use server" on top. JUST make sure to write "use server" for server actions that you created, thats it. |
just mark "use server" in all your server actions , that's all you need to do. |
Prerequisites
Mongoose version
8.0.4
Node.js version
20.9.0
MongoDB version
6.3.0
Operating system
None
Operating system version (i.e. 20.04, 11.3, 10)
11
Issue
While connecting to MongoDB it throws this error
and the code that throws this error is below while calling connectToDatabase() in other file
I've cleared the cache, reinstalled all node packages still the problem prevail. Kindly look into this. Also saw a similar thread at #14183 but its a temp solution.
The text was updated successfully, but these errors were encountered: