TypeError: Converting circular structure to JSON when using axios
TypeError: Converting circular structure to JSONShort 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.
Fix it fast
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));2Match what you see, then jump to its fix
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 }).lengthConfirmed by axios.rest
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
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix