Fix TypeError: Cyclic object value...

Learn how to resolve the TypeError in JavaScript. Converting circular structure to JSON. Use a cycle...

javascripttypeerrordebugging

The Error

The TypeError: Cyclic object value error occurs when JavaScript attempts to convert an object that contains a circular reference into a JSON string. In other words, this error is raised when the JavaScript engine encounters an object that references itself, either directly or indirectly, during the serialization process.

Why it occurs

This error commonly occurs in the following scenarios:

Example Code

The following example demonstrates how this error can occur:

const obj = {
  name: 'John',
  age: 30,
  friends: []
};

const friend = {
  name: 'Jane',
  age: 25
};

obj.friends.push(friend);
friend.spouse = obj; // Create a circular reference

try {
  const json = JSON.stringify(obj);
  console.log(json);
} catch (error) {
  console.error(error); // TypeError: Cyclic object value
}

In this example, the obj object references the friend object, and the friend object references the obj object, creating a circular reference. When we attempt to serialize the obj object using JSON.stringify(), the JavaScript engine throws a TypeError: Cyclic object value error.

How to Fix

To fix this error, you can use a cycle-handling library or remove the cycles from your object. Here are the steps:

Using a cycle-handling library

One popular library for handling circular references is json-stringify-safe. You can install it using npm:

npm install json-stringify-safe

Then, use the stringify function from the library to serialize your object:

const stringify = require('json-stringify-safe');

const obj = {
  name: 'John',
  age: 30,
  friends: []
};

const friend = {
  name: 'Jane',
  age: 25
};

obj.friends.push(friend);
friend.spouse = obj; // Create a circular reference

const json = stringify(obj);
console.log(json);

Removing cycles

Alternatively, you can remove the cycles from your object before serializing it. One way to do this is to use a recursive function to detect and remove circular references:

function removeCycles(obj, seen = new WeakSet()) {
  if (typeof obj !== 'object' || obj === null) {
    return obj;
  }

  if (seen.has(obj)) {
    return undefined; // Remove the circular reference
  }

  seen.add(obj);

  if (Array.isArray(obj)) {
    return obj.map(item => removeCycles(item, seen));
  }

  return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, removeCycles(value, seen)]));
}

const obj = {
  name: 'John',
  age: 30,
  friends: []
};

const friend = {
  name: 'Jane',
  age: 25
};

obj.friends.push(friend);
friend.spouse = obj; // Create a circular reference

const cleanedObj = removeCycles(obj);
const json = JSON.stringify(cleanedObj);
console.log(json);

Best Practices

To avoid this error in the future, follow these best practices:

Found a typo? Edit this page on GitHub

Related Errors