Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Mongoose throws OverwriteModelError for a model name

ErrorOverwriteModelError: 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.

  • Applies to Node.js 22, Mongoose 8, Express 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/null
  2. 2Match what you see, then jump to its fix

    1. 1Two 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. 2The 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. 3The 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. 4You intentionally want the same model on two databasesSeveral connections need the same model nameFix

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

Confirmed by mongoosejs.com

Did this fix it?

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/ | head

2Fix

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 test

Confirmed by mongoosejs.com

Did this fix it?

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/null

2Fix

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 dev

Confirmed by mongoosejs.com

Did this fix it?

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

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.