Skip to content
>_VibeUnstuck

Unexpected token '<' is not valid JSON

Last verified:

The fix

Unexpected token '<' is not valid JSON: res.json() read HTML, usually a 404 or index.html. Check the status and URL first. A <!DOCTYPE body is not JSON.

  1. 1

    Log the status, the content type, and the first characters

    Read the body once with res.text(). Log res.status, the content-type header, and the first characters of the text. Do not call .json() and .text() on the same response. The body stream can be read once.

    const res = await fetch(url);
    const contentType = res.headers.get("content-type") ?? "";
    const raw = await res.text();
    console.log(res.status, contentType, raw.slice(0, 120));
  2. 2

    Check the URL the browser actually requested

    In the network panel, compare the request URL with the route you think you wrote. Check the path, a missing or extra /api prefix, and the base URL used in production. A relative /api/items on the deployed site is not the localhost path that worked in next dev.

  3. 3

    Confirm the route exists in the deployed build

    Open the built output. A static export writes files. It does not run a POST handler. If the route was only a server function, it is absent after output: 'export', and the host answers with HTML. A Vite app deployed as files has the same gap unless something else serves that path.

  4. 4

    See whether you were sent to a login page

    A 302 or 307 whose Location is /login, or a 200 whose body is the login HTML, means the API call hit a guard. Fix the session or exclude the API path from the redirect. Do not parse that HTML as JSON. The login-redirect page covers the loop itself.

  5. 5

    Parse JSON only when the response is JSON

    Call JSON.parse on the text only when the status is one you handle and the content type contains application/json. Otherwise throw an error that includes the status and the first characters, so the next read shows the HTML instead of a parse exception.

    if (!res.ok || !contentType.includes("application/json")) {
      throw new Error(
        `Expected JSON, got ${res.status} ${contentType}: ${raw.slice(0, 120)}`,
      );
    }
    const data = JSON.parse(raw);

Why it happens

Response.json() reads the body and parses it as JSON. MDN documents that this throws SyntaxError when the body cannot be parsed as JSON. A body that starts with <!DOCTYPE is HTML. The request may still have completed. The server, or the host in front of it, sent a page instead of an object.

That page is usually a 404, a 500, an auth redirect to a login screen, or a single-page app fallback that returns index.html with status 200 for a path that has no file. A static Next.js export (output: 'export') has no Node server. Pages Router API routes are unsupported, and App Router route handlers that read the incoming request are unsupported. A fetch to /api/... on that host often receives the 404 HTML, and .json() then throws this error.

The next error you'll probably hit

Once this is fixed, the usual follow-up is 404 on refresh of a client route. A 404 on refresh means the Vite app works from /, then a reload of /dashboard misses index.html. Rewrite unknown paths to index.html on the host.

Prompt to paste back into your AI tool

Paste this above the error.

The browser throws: Unexpected token '<', "<!DOCTYPE "... is not valid JSON. The code calls res.json() on a fetch.

Do not wrap the parse in an empty catch. Do not disable auth or RLS to make the call succeed. Do not put a secret in a NEXT_PUBLIC_ or VITE_ variable.

1. Stop editing. Log res.status, the content-type header, and the first 120 characters of res.text(). Read the body only once.
2. State the exact request URL and whether that route exists in the deployed build. A static export has no server API route.
3. If the body is a login page or a 404 page, say so. Do not JSON.parse HTML.
4. Parse only when the content type is JSON. Include the status in the error you throw.
5. Change only the fetch call and, if the route is missing, the one handler that should answer it.

Common questions

+The status is 200 and the body is still HTML. Why?

A single-page host often rewrites every unknown path to index.html and returns 200. The browser then tries to parse that document as JSON. Check the request path, and see the SPA refresh 404 page for the rewrite.

+It works in next dev and fails after deploy. Why?

next dev is a server. A static export is files. Route handlers that read the request, and Pages Router API routes, are not part of that export, so the host returns its HTML error page.

Related errors