This is a parse error, not a runtime error: the JavaScript engine read the file, reached a character or word it could not fit into the grammar at that spot, and stopped before running anything. The message names the offending token and usually a position. Most cases are a genuine typo near that spot, but one famous variant has nothing wrong with the code at all: the file being parsed is not JavaScript, it is an HTML error page or a JSON document fed to the wrong parser.
{ } ( ) [ ] in the function or object around the spot. An editor's bracket-matching (click a bracket, its partner highlights) finds the imbalance quickly.< and the code looks fine, the “JavaScript” being parsed is probably an HTML page, typically a 404 or login page a server returned for a script or JSON request.Unexpected token '<' after a fetch means the response was HTML, not JSON. Unexpected token ':' often means JSON text was evaluated as code.<, inspect the network response, not the code. Log response.status and the first characters of the body before parsing, and fix the URL or the server route that returned a page instead of data.JSON.parse(text) or response.json(), never eval or a script tag, and validate untrusted JSON separately if it came from a file.Why does the error point at a line that looks correct? The parser reports where it became impossible to continue, which can be several tokens after an unclosed string, comment, or bracket. Inspect upward from the reported position for the first construct that never closes.
What does “Unexpected token '<', "..." is not valid JSON” mean? A fetch expected JSON and received an HTML page instead, usually a 404, a redirect to a login page, or a server error page. The bug is in the request or the server, not in the JSON handling.
Can an invisible character really cause this? Yes. Curly quotes and non-breaking spaces pasted from documents or web pages are not valid JavaScript punctuation and produce exactly this error at that character.