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
Can confirm - removing
retryWritesmade 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:Once I removed the uri options the issue went away without any other code changes:
Currently experiencing the same issue. Any idea how to resolve this.
Works on Atlas: MongoDB cluster without
?retryWrites=trueI ran into this issue as well updating from Mongoose 4.11.7 to 5.1.4. I tried removing
?retryWrites=truefrom my connection URI, no luck. Finally I figured out I was usingindex:trueon a custom_idproperty like this:Removing
index:truefixed 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: falseintomongoose.connect- but doing so my indexes were not being created anymore even after usingensureIndex()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
after
AND, more importantly, removing attribute INDEX from compound indexes, i.e.:
before
after
hope this helps others cheers
Confirmed! removing
&retryWrites=truetrully fixes the errorI can confirm that removing
&retryWrites=truefrom the connection URI fixes the errorFound the issue. It has nothing to do with Atlas, or
retryWrites, orfeatureCompatibilityVersion. It has everything to do with trying to create an index on_id. Minimal repro script:Will open up a PR in the mongodb driver for this
With this code from @mykhaliuk :
I got the following error:
Error: cyclic dependency detectedThe only way I found to maintain the
retryWritesis using the functioncreateIndexesfrom mongodb API:So we are removing
unique,indexfrom Mongoose model definitions and creating all indexes using createIndexes (https://docs.mongodb.com/manual/reference/command/createIndexes/)Will investigate and check whether turning on
retryWritescauses 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 likenew Schema({ _id: {type: String, index: true} })orschema.index({ _id: 1 })please remove that.I’m specifically encountering this issue when attempting to add index on an array:
Simply adding that prop/index to my schema is causing this issue