Attempting to parse a string as JSON using JSON.parse() when the string is actually an HTML page or response.
This error occurs when JSON.parse() receives an invalid JSON string starting with the character '<'. This almost always happens when fetching from an API endpoint that returned an HTML page instead of JSON.
Passing raw API responses directly to response.json() or JSON.parse() without checking the HTTP status code or Content-Type header first.
1fetch('/api/user-profile')2 .then(res => res.text()) // returns HTML error page: '<!DOCTYPE html>...'3 .then(text => {4 const data = JSON.parse(text); // SyntaxError: Unexpected token < in JSON at position 05 });1fetch('/api/user-profile')2 .then(res => {3 // Fix: Verify response status and Content-Type before parsing JSON4 if (!res.ok) {5 throw new Error(`Server returned HTTP ${res.status}`);6 }7 const contentType = res.headers.get("content-type");8 if (contentType && contentType.includes("application/json")) {9 return res.json();10 }11 throw new Error("Received non-JSON response from server");12 })13 .then(data => {14 console.log("Profile data:", data);15 })16 .catch(err => console.error("Error loading profile:", err.message));Simulate standard system builds to trigger compiler trace records and track memory crashes locally.
The server encountered an error and returned a standard HTML error page (starting with '<!DOCTYPE html>'). Passing this text to JSON.parse() fails immediately because HTML syntax is not valid JSON syntax.