Maaz Zindani

Loading the index…

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

TypeError: Converting circular structure to JSON when using axios

ErrorTypeError: Converting circular structure to JSON

Short answer

JSON.stringify cannot write an object that points back to itself. With axios this almost always means you passed the whole response (or error) instead of response.data, because it holds the request object. If the data is your own, remove the back-reference or serialize with a replacer.

  • Applies to Node.js 22, axios 1.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the extra lines V8 can add under the error. They name the constructor and the property that closes the loop. Then log the keys of the value you are serializing.

    console.log(Object.keys(value));
  2. 2Match what you see, then jump to its fix

    1. 1Object.keys shows config and request next to dataYou are serializing the axios response object, not its dataFix
    2. 2The loop names one of your own properties, for example parent or ownerYour own object contains a reference back to its parentFix

What does the error look like?

  • TypeError: Converting circular structure to JSON
  • --> starting at object with constructor 'Object'
  • --- property 'myself' closes the circle

Cause 1 of 2

You are serializing the axios response object, not its data

1Check

An axios response has data, status, statusText, headers, config and request. In Node.js the request field is the last http.ClientRequest, a live network object that is not plain data and not meant for JSON. If Object.keys shows these names, you have the whole response.

const response = await axios.get('https://example.com/api/items');
console.log(Object.keys(response));

2Fix

Send or store response.data. If you need the status as well, build a small plain object with only the fields you want.

const response = await axios.post(url, payload);
res.json({ status: response.status, data: response.data });

3Verify

The call that failed now returns without a TypeError.

JSON.stringify({ status: response.status, data: response.data }).length

Confirmed by axios.rest

Did this fix it?

Cause 2 of 2

Your own object contains a reference back to its parent

1Check

JSON has no syntax for references, so any object that reaches itself through a chain of properties fails. Find the property V8 names in the message.

node -e "const a = { n: 1 }; a.self = a; try { JSON.stringify(a) } catch (e) { console.log(e.message) }"

2Fix

Leave the back-reference out when you serialize, either by building a plain object or with a replacer function that skips that key.

const json = JSON.stringify(node, (key, value) => (key === 'parent' ? undefined : value));

3Verify

JSON.stringify returns a string instead of throwing.

console.log(typeof json);

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Pass response.data (or a small object you build) around your code, never the response itself.
  • Keep data you plan to send as JSON free of parent pointers; keep those on the in-memory model only.
  • structuredClone copies cycles but does not make JSON, so it does not replace the fix above.

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

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

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