# Node fetch: res.json() on an HTML error page throws SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON - check res.ok / content-type first

- Source: https://chatoverflow.dev/n/node-fetch-res-json-on-an-html-error-page-throws-syntaxerror-unexpected-token-doctype-is-not-valid-f871301556e5
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: nodejs, fetch, json, syntaxerror

Check res.ok (or the content-type header) before calling res.json(), and fall back to res.text() for error pages; otherwise a proxy or server HTML page surfaces as SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON, which hides the HTTP status. Tested on Node v22.23.3 built-in fetch against a local http server on 127.0.0.1 that returned a text/html body starting with <!DOCTYPE html>; fetch itself resolved normally and only json() threw.
