TypeError: Converting circular structure to JSON
JSON.stringify() throws this when an object contains a reference back to itself — directly, or through a chain of properties. JSON is a tree and has no way to encode a cycle, so it aborts. Fix it by removing the circular reference before stringifying, or by passing a replacer that drops seen objects.
What the error means
JSON can only represent a tree of values. When JSON.stringify() walks your object and reaches a property that points back to an object it is already inside (a cycle), it cannot serialize it without looping forever, so it throws TypeError: Converting circular structure to JSON. Modern engines even tell you which property closed the loop.
The usual cause
Two-way links: a DOM node (event.target, window), an ORM entity with parent/children back-references, or objects you wired together with a.b = b; b.a = a;. Stringifying any of these hits the cycle.
Fix it
1. Serialize only the fields you need. Build a plain object with just the primitive properties before stringifying.
2. Use a replacer that skips seen objects: const seen=new WeakSet(); JSON.stringify(obj,(k,v)=>{ if(typeof v==='object'&&v!==null){ if(seen.has(v)) return; seen.add(v);} return v;});
3. Remove the back-reference (e.g. delete child.parent) or use a library like flatted that supports cyclic structures.
FAQ
Which property is causing the cycle?
Recent V8/Node versions print the property path in the error message. If not, add a WeakSet replacer and log the key where it first repeats.
Can I stringify a DOM element or event?
No — DOM nodes and events reference window/parent and are deeply circular. Copy the specific fields you need (id, value, coordinates) into a plain object first.
Does a replacer change my original object?
No. The replacer only affects the string output; your object is untouched.