SyntaxError: "undefined" is not valid JSON in a React app
SyntaxError: "undefined" is not valid JSONShort answer
JSON.parse turns any non-string argument into a string first, so undefined becomes the text undefined, which is not JSON. Find the variable you pass in. It is usually a missing prop, context value or stored item. Check the type before parsing, and never store the result of JSON.stringify(undefined).
Fix it fast
1Find out which cause you have
Open the stack trace and find the JSON.parse call. Log the value and its type right before it.
console.log(typeof value, value); JSON.parse(value);2Match what you see, then jump to its fix
What does the error look like?
SyntaxError: "undefined" is not valid JSON
Cause 1 of 2
The value reaching JSON.parse is undefined
1Check
MDN documents that the argument is converted to a string, so undefined is parsed as the four letters undefined. Trace where the variable comes from: a prop, a useContext value or an object field that does not exist.
console.log(typeof value, value);
// for a context: check the Provider wraps this component2Fix
Parse only when you have a string, and give the value a default.
function safeParse(text, fallback = null) {
if (typeof text !== 'string') return fallback;
try {
return JSON.parse(text);
} catch (err) {
if (err instanceof SyntaxError) return fallback;
throw err;
}
}3Verify
Reload with the data missing. The component renders the fallback and the console shows no SyntaxError.
safeParse(undefined) // nullConfirmed by developer.mozilla.org
Cause 2 of 2
Something undefined was saved with JSON.stringify
1Check
JSON.stringify(undefined) returns undefined, not a string, and Web Storage holds strings only. So a write like setItem(key, JSON.stringify(state)) is only safe when state is defined.
console.log(JSON.stringify(undefined)); // undefined
console.log(localStorage.getItem('user'));2Fix
Skip the write when the value is undefined, and remove any bad key already stored.
if (state !== undefined) {
localStorage.setItem('user', JSON.stringify(state));
} else {
localStorage.removeItem('user');
}3Verify
Reload the page. getItem returns null for a missing key, and JSON.parse(null) gives null without an error.
localStorage.getItem('user')Confirmed by developer.mozilla.org
How do I stop it coming back?
- Wrap every JSON.parse of external data (storage, URL, props) in one helper that checks the type and catches SyntaxError.
- Give React context a real default value, or throw a clear error when the Provider is missing.
- Write to storage only after checking the value is not undefined.
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix