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

**Code:** `OverwriteModelError: Cannot overwrite model once compiled` · **Category:** MongoDB · **Seen on:** MongoDB, Node.js

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

## 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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/mongodb/mongoose-model-overwrite-error)
