OverwriteModelError: Cannot overwrite model once compiled MongoDB Origin: Data Last updated 5 Nov 2026

Mongoose: OverwriteModelError: Cannot overwrite `User` model once compiled.

mongoose.model('User', schema) was called twice with the same name, typically from hot reloading.

Exact message you may see OverwriteModelError: Cannot overwrite `User` model once compiledCannot overwrite model once compiledOverwriteModelError Cannot overwrite model once compiled mongooseMongoose Cannot overwrite model once compiled Next.js
Seen on: MongoDB Node.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

  1. `export default mongoose.models.User
  2. mongoose.model('User', userSchema);`
  3. Ensure one definition per model name
  4. In tests, mongoose.deleteModel('User') before redefining

Detailed fix by platform

Node.js

  1. `const User = mongoose.models.User
  2. mongoose.model("User", schema);`

How to diagnose

  1. Reload — Hot reload/dev server?
  2. Duplicates — Defined twice?
  3. 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:

Where this error sits

Origin: Data. Errors that are often confused with or caused by this one (click to open):

CastError ObjectId — CastError: Cast to ObjectId failed for value "x" (type string) at path "_id" (Node.js)CastError ObjectIdCastError: Cast to Date/Number/Boolean failed — Mongoose: CastError: Cast to date failed for value "abc" (type string) at path "createdAt" (MongoDB)CastError: Cast to Da…MissingSchemaError: Schema hasn't been registered for model — Mongoose: MissingSchemaError: Schema hasn't been registered for model "User". Use mongoose.model(name, schema) (MongoDB)MissingSchemaError: S…Mongoose ValidationError — ValidationError: User validation failed: email: Path `email` is required. (Node.js)Mongoose ValidationEr…Mongoose: Model.find() no longer accepts a callback — MongooseError: Model.find() no longer accepts a callback (MongoDB)Mongoose: Model.find(…Mongoose: strictQuery will be switched back to false — Mongoose: [MONGOOSE] DeprecationWarning: Mongoose: the strictQuery option will be switched back to false by default in Mongoose 7 (MongoDB)Mongoose: strictQuery…MongoParseError: options usenewurlparser / useunifiedtopology not supported — MongoParseError: option usenewurlparser is not supported (MongoDB)MongoParseError: opti…NamespaceNotFound: ns not found — MongoServerError: ns not found (NamespaceNotFound) — dropping/renaming/collStats on a missing collection (MongoDB)NamespaceNotFound: ns…Overwrite…

Explore the map of this error Full error map →

🧠 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.