mongoose: Error: cyclic dependency detected

Running the latest version of Node.js on macOS. Also running the latest version of Mongoose.

Seeing Error: cyclic dependency detected with literally no way to debug. Everything points back to Mongoose. Any suggestions? Started happening today.

/Users/nickparsons/Code/winds-2.0/api/src/node_modules/mongoose/lib/utils.js:417
        throw err;
        ^

Error: cyclic dependency detected
    at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:296:33)
    at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:618:17)
    at serializeObject (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/Users/nickparsons/Code/winds-2.0/api/src/node_modules/bson/lib/bson/parser/serializer.js:776:17)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 6
  • Comments: 40

Most upvoted comments

Can confirm - removing retryWrites made the issue go away, even with the “old” mongodb:// URI.

Might be worth noting, I was using the mongodb+srv:// uri structure with a few options:

mongodb+srv://user:password@host.mongodb.net?readPreference=primaryPreferred&retryWrites=true

Once I removed the uri options the issue went away without any other code changes:

mongodb+srv://user:password@host.mongodb.net

Currently experiencing the same issue. Any idea how to resolve this.

Works on Atlas: MongoDB cluster without ?retryWrites=true

I ran into this issue as well updating from Mongoose 4.11.7 to 5.1.4. I tried removing ?retryWrites=true from my connection URI, no luck. Finally I figured out I was using index:true on a custom _id property like this:

_id: {
    type: String,
    required: true,
    index: true
  }

Removing index:true fixed the issue for me. Also found this issue which mentions the solution : https://github.com/Automattic/mongoose/issues/2303. Just posting incase someone else runs into the same issue.

guys, I guess I found the cause - apparently it is related to mongodb 3.6 (I recently upgraded from 3.2 to 3.6 and mongoose got upgraded to 5.x as well)…

so I was not being able to run my app anymore, I was getting these cyclic dependency errors… the only work-around that worked for me was to add autoIndex: false into mongoose.connect - but doing so my indexes were not being created anymore even after using ensureIndex() later on the application - it would eventually result in the same cyclic dependency errors…

long story short I managed to fix it by removing all attributes INDEX from inside my models and appending them to the model afterwards, like this:

before

client: { type: ObjectId, ref: 'tsCli', required: true, index: true },

after

client: { type: ObjectId, ref: 'tsCli', required: true },
[...]
Schema.index({ client: 1 });

AND, more importantly, removing attribute INDEX from compound indexes, i.e.:

before

Schema.index({ client: 1, code: 1 }, { unique: true, index: true, partialFilterExpression: { code: { $type: String } } });

after

Schema.index({ client: 1, code: 1 }, { unique: true, partialFilterExpression: { code: { $type: String } } });

hope this helps others cheers

Confirmed! removing &retryWrites=true trully fixes the error

I can confirm that removing &retryWrites=true from the connection URI fixes the error

Found the issue. It has nothing to do with Atlas, or retryWrites, or featureCompatibilityVersion. It has everything to do with trying to create an index on _id. Minimal repro script:

const mongoose = require('mongoose');
mongoose.set('debug', true);

Error.stackTraceLimit = Infinity;

const { Schema } = mongoose;

run().then(() => console.log('done')).catch(error => console.error(error.stack));

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test', {dbName: 'test'});
  
  const M = mongoose.model('Test', new Schema({ _id: { type: String, index: true } }));

  await M.create({ _id: 'abc' + Math.random() });
  console.log(await M.findOne());
}

Will open up a PR in the mongodb driver for this

With this code from @mykhaliuk :

const mongoose = require('mongoose');
mongoose.set('debug', true);

const { Schema } = mongoose;

run().then(() => console.log('done')).catch(error => console.error(error.stack));

async function run() {
  await mongoose.connect('mongodb+srv://user:pw@omitted.mongodb.net/test?retryWrites=true', {dbName:'db-name'});

  const M = mongoose.model('Test', new Schema({ name: {type: String, unique: true }}));

  await M.create({ name: 'test' });
  console.log(await M.findOne());
}

I got the following error: Error: cyclic dependency detected

The only way I found to maintain the retryWrites is using the function createIndexes from mongodb API:

const mongoose = require('mongoose');
mongoose.set('debug', true);

const { Schema } = mongoose;

run().then(() => console.log('done')).catch(error => console.error(error.stack));

async function run() {
  await mongoose.connect('mongodb+srv://user:pw@omitted.mongodb.net/test?retryWrites=true');

  const M = mongoose.model('NewTest', new Schema({ name: {type: String} }));

  M.collection.createIndexes([{key: {name: 1}, name: "name", unique: true}])

  await M.create({ name: 'test' });
  console.log(await M.findOne());
}

So we are removing unique, index from Mongoose model definitions and creating all indexes using createIndexes (https://docs.mongodb.com/manual/reference/command/createIndexes/)

Will investigate and check whether turning on retryWrites causes this crash.

I encountered the same issue when using atlas, but everything was fine locally. A quick fix around it, as someone suggested is removing the parameter ?retryWrites=true , and now things are fine.

@AndrewBarba thanks for the repro, I found another issue like node-mongodb-native#1740, see https://github.com/mongodb/node-mongodb-native/pull/1758 . This fix will end up in mongoose once a new version of the mongodb driver is released.

Try removing any indexes you have on _id @otisidev . So if you have something like new Schema({ _id: {type: String, index: true} }) or schema.index({ _id: 1 }) please remove that.

I’m specifically encountering this issue when attempting to add index on an array:

...
tags: {
  type: [String],
  index: true
}
...

Simply adding that prop/index to my schema is causing this issue