Mongoose throws OverwriteModelError for a model name
OverwriteModelError: Cannot overwrite `User` model once compiled.Short answer
Mongoose models are registered by name, and each name can be compiled only once per connection. The error means mongoose.model('Name', schema) ran a second time. Look for the same name in two files, a model file loaded again by watch mode, tests or hot reload, or a second connection.
Fix it fast
1Find out which cause you have
Find every place that compiles a model with that name. More than one hit is the cause already.
grep -rn "mongoose.model(" --include=*.js --include=*.ts src/ models/ test/ 2>/dev/null2Match what you see, then jump to its fix
- 1
Two grep hits show the same first argument, for example two files both calling model('User', ...)The same model name is compiled in two filesFix - 2
The first test passes and a later one fails, or the error appears after saving a file under a watcherA test or watch run compiles the model again in the same processFix - 3
The error only appears in development after you save a file, in a framework such as Next.jsHot reload in a framework re-runs the model fileFix - 4
You intentionally want the same model on two databasesSeveral connections need the same model nameFix
- 1
What does the error look like?
OverwriteModelError: Cannot overwrite `User` model once compiled.MongooseError: Cannot overwrite model once compiled
Cause 1 of 4
The same model name is compiled in two files
1Check
model name must be unique, and compiling it again throws. A copied model file is the usual way to get a second one.
grep -rn "mongoose.model('User'" src/2Fix
Keep one definition and import it everywhere, or rename the second model.
// models/User.js: the only definition
module.exports = mongoose.model('User', userSchema);
// elsewhere
const User = require('./models/User');3Verify
The app starts without the error.
node server.jsConfirmed by mongoosejs.com
Cause 2 of 4
A test or watch run compiles the model again in the same process
1Check
A hook such as beforeEach that runs the model definition again, or a module cache that is cleared while Mongoose keeps its registry, compiles the name a second time.
grep -rn "beforeEach\|jest.resetModules\|delete require.cache" test/ | head2Fix
Define the model once at module level, and fetch an existing one with mongoose.model('Name') when needed.
const User = mongoose.models.User || mongoose.model('User', userSchema);3Verify
Run the whole suite twice in one go.
npm testConfirmed by mongoosejs.com
Cause 3 of 4
Hot reload in a framework re-runs the model file
1Check
Hot reloading evaluates modules again while the Mongoose instance, and its model registry, stays alive.
grep -rn "mongoose.model(" app/ models/ lib/ 2>/dev/null2Fix
Register the model only when it does not exist yet. This is the pattern in the Mongoose Next.js guide.
export default mongoose.models.User || mongoose.model('User', userSchema);3Verify
Edit and save a file; no error in the dev server.
npm run devConfirmed by mongoosejs.com
Cause 4 of 4
Several connections need the same model name
1Check
mongoose.model uses the default connection. A second connection created with createConnection needs its own registration.
grep -rn "createConnection" --include=*.js .2Fix
Compile the model on each connection with that connection's model function.
const conn = mongoose.createConnection(uri2);
const Kitten = conn.model('Kitten', kittenSchema);3Verify
Both connections can query their Kitten collection.
node -e "console.log(Object.keys(require('mongoose').models))"Confirmed by mongoosejs.com
How do I stop it coming back?
- One file per model, imported everywhere, never redefined.
- In frameworks with hot reload, use the registered-or-new pattern.
- Use connection.model when one app talks to more than one database.
- Keep model names unique across all packages that use the same mongoose instance.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fixBack end
Browser blocks a stylesheet or script: MIME type mismatch with nosniff
The Network tab shows the asset URL answered with Content-Type text/html
With X-Content-Type-Options: nosniff the browser accepts a stylesheet only as text/css and a script only as a JavaScript type.
3 causes, each with a fix