Mongoose: OverwriteModelError: Cannot overwrite `User` model once compiled.
mongoose.model('User', schema) was called twice with the same name, typically from hot reloading.
OverwriteModelError: Cannot overwrite `User` model once compiledCannot overwrite model once compiledOverwriteModelError Cannot overwrite model once compiled mongooseMongoose Cannot overwrite model once compiled Next.js Meaning
Reuse the existing model: mongoose.models.User || mongoose.model('User', schema). Frequent in Next.js/serverless dev with hot reload, or when two files define the same model name. In Next.js place the guard in the model file.
Common causes
- Hot reload re-evaluating the model file
- Two files registering the same model name
- Test suites re-importing models
- Serverless warm start duplicating modules
- Case difference in file names importing twice
⚡ Quick fix
- `export default mongoose.models.User
- mongoose.model('User', userSchema);`
- Ensure one definition per model name
- In tests,
mongoose.deleteModel('User')before redefining
Detailed fix by platform
Node.js
- `const User = mongoose.models.User
- mongoose.model("User", schema);`
How to diagnose
- Reload — Hot reload/dev server?
- Duplicates — Defined twice?
- Guard — mongoose.models used?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- CastError ObjectId CastError: Cast to ObjectId failed for value "x" (type string) at path "_id"
- CastError: Cast to Date/Number/Boolean failed Mongoose: CastError: Cast to date failed for value "abc" (type string) at path "createdAt"
- MissingSchemaError: Schema hasn't been registered for model Mongoose: MissingSchemaError: Schema hasn't been registered for model "User". Use mongoose.model(name, schema)
Most viewed in MongoDB
- MongoParseError MongoDB: MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"
- Document failed validation MongoDB: MongoServerError: Document failed validation (code 121)
- bad auth : Authentication failed MongoDB: MongoServerError: bad auth : Authentication failed (code 8000 / 18)
Other ways to find it
Where this error sits
Origin: Data. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 5 Nov 2026