"Unexpected token '<', "<!doctype "... is not valid JSON": how to fix it
It works on your machine, then the live app says SyntaxError: Unexpected token '<', "<!doctype "... is not valid JSON (or Unexpected token '<' in JSON at position 0). Your code called response.json(), but the server sent back a web page. The < is the first letter of <!doctype html>. The fix is to find out which request got HTML and why.
Stuck on this? Get it fixed for €49.
Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.
Get it fixed for €491. Find the request that returned HTML
Open DevTools → Network, reload, and click the request made just before the error. Look at its URL, status code and the Response tab. If you see your app's own page, a "404 Not Found" page, a host error page or a login screen, that is your answer. The steps below cover each case.
2. The API URL is wrong or relative in production
A call like fetch('/api/items') works in development because the dev server (or a Vite proxy) forwards it to your backend. In production there is no proxy, so the request hits your static site, and the host answers with index.html. Either deploy the API on the same domain under /api, or call the full backend URL from an environment variable:
const API = import.meta.env.VITE_API_URL; // e.g. https://api.example.com
const res = await fetch(`${API}/items`);
Note: a Vite server.proxy setting only works in npm run dev. It does nothing once the app is built.
3. An environment variable is missing on the host
If the base URL comes from an env var that was never added to Vercel, Netlify or Lovable's settings, the request goes to undefined/items, which is a relative path, so you get your own HTML page back. Add the variable on the host (with the VITE_ or NEXT_PUBLIC_ prefix if the browser needs it) and redeploy. See environment variables undefined in production.
4. Your SPA rewrite is catching /api
A catch-all rewrite that sends every path to index.html also catches API routes if they are listed after it, or if the rule has no exception. Put API routes first, or exclude them:
// vercel.json
{
"rewrites": [
{ "source": "/((?!api/).*)", "destination": "/index.html" }
]
}
On Netlify, keep /api/* redirects above the /* /index.html 200 rule in _redirects.
5. The endpoint does not exist (a 404 page)
A typo, a missing trailing path, or a serverless function that was never deployed returns the host's 404 page as HTML. Open the exact URL from step 1 in a new tab. On Vercel, check the deployment's Functions list to confirm the route was built. For Supabase Edge Functions, check the function name in supabase.functions.invoke('name') matches what you deployed.
6. You got a login, error or "please wait" page
Vercel Deployment Protection, a Cloudflare challenge, or an expired session that redirects to /login all return HTML. Preview URLs on Vercel are often protected by default, so test against your production domain, and make sure API calls send the auth header they need.
7. Make the error tell you what happened
Check the status and content type before parsing, so the next problem shows a clear message instead of a JSON crash:
const res = await fetch(url);
const type = res.headers.get('content-type') || '';
if (!res.ok || !type.includes('application/json')) {
const text = await res.text();
throw new Error(`${res.status} from ${url}: ${text.slice(0, 120)}`);
}
const data = await res.json();
Quick checklist
- Found the request in Network and saw HTML in its response
- Production calls the real API URL, not a dev-only proxy path
- Every env var used in the URL is set on the host, then redeployed
- SPA rewrite excludes
/api - Endpoint URL opens in a new tab and returns JSON
- No protection, challenge or login page in front of the API
Stuck on this? Get it fixed for €49.
Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.
Get it fixed for €49Still stuck?
Mendlark fixes this kind of problem for a fixed €49 (launch price, normally €79). We agree the problem first, test the fix and send it as a pull request, so nothing changes in your app until you merge it. If we can't fix it, you don't pay. Get the €49 fix, or start with a free scan.
Not sure what else is wrong? Paste your app link into the free instant check and get a plain-English list of problems in seconds.