chrome 111+ · JavaScript
The message
SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
What it means
What you handed to JSON.parse was HTML, not JSON. The <!DOCTYPE fragment quoted in the message is the proof, and it means the server answered with an HTML page — a 404, a 502, or a login redirect — so the real bug is upstream of the parse, in the request. Typical causes: the wrong URL, a dev-server proxy that served index.html instead of the API, or an expired session that redirected you to a login page. Check res.ok before calling res.json(), and read res.text() to see what actually arrived; hitting the URL with curl -s is the fastest way to look. Before Chrome 111 and Node 20 the same error read Unexpected token < in JSON at position 0.
The fix
curl -s https://api.example.com/data | head -5- Printed by
- chrome 111+
- JavaScript
- 15
Browser and Node errors usually tell you what broke and not why the value became what it was — you read undefined, it was not a function, it was not JSON — so the place to fix is upstream of the line that threw, and silencing just that line with ?. and default values makes the same problem reappear further away in a form that is harder to recognise.
Reading an error message
- Read from the first line down. The lower you go the more it is about the tool’s internals; the cause is usually at the top.
- If there is a file and a line number, start there — not the top stack frame, but the topmost line that names a file you wrote.
- Search the message verbatim, but strip your own paths and variable names first; those are what stop the search from matching.
- The same condition is worded differently across tool versions. If results look wrong, add the version number to the query.
- Before pasting a fix, check what it throws away. Some of these cannot be undone.
Common questions
Q. What does “SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON” mean?
What you handed to JSON.parse was HTML, not JSON. The <!DOCTYPE fragment quoted in the message is the proof, and it means the server answered with an HTML page — a 404, a 502, or a login redirect — so the real bug is upstream of the parse, in the request. Typical causes: the wrong URL, a dev-server proxy that served index.html instead of the API, or an expired session that redirected you to a login page. Check res.ok before calling res.json(), and read res.text() to see what actually arrived; hitting the URL with curl -s is the fastest way to look. Before Chrome 111 and Node 20 the same error read Unexpected token < in JSON at position 0.
Q. How do I fix it?
curl -s https://api.example.com/data | head -5 — before running it, check the explanation above for what this command discards.
Q. Which tool prints this?
chrome 111+. It sits under JavaScript, and the message runs to 10 words.