Skip to main content

Browser · 5 common causes

Uncaught SyntaxError: Unexpected token

Updated

The browser tried to parse something that is not valid JavaScript — most often an HTML error page served where a script or JSON was expected.

The exact message

app.js:1  Uncaught SyntaxError: Unexpected token '<'

// or, from a fetch:
Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is Uncaught SyntaxError: Unexpected token.

How to fix it

Cause 1 of 5

The script URL 404'd and the server returned an HTML page.

The < is the first character of <!DOCTYPE html>. Open the failing URL in the Network tab and look at the response body — you will see the error page. Fix the path or the base URL; a missing leading slash after a routing change is the usual culprit.

Cause 2 of 5

You called res.json() on a response that is not JSON.

Same root cause from the fetch side. Check res.ok and the content-type header before parsing, and log await res.text() when it fails so the actual body is visible instead of a parse error.

Cause 3 of 5

JSX or TypeScript is being served without a build step.

Browsers cannot parse either. Make sure the file goes through the bundler; a <script src> pointing straight at a .jsx or .ts file will always fail on the first piece of syntax the browser does not know.

Cause 4 of 5

There is a genuine syntax mistake in the file.

When the token named is ), }, , or ; the source really is malformed. The reported line is where the parser could first tell something was wrong — the actual mistake is usually a line or two above, typically a missing bracket or comma.

Cause 5 of 5

A server-side template placeholder was never substituted.

An unrendered {{ value }} or a PHP tag left in a script block lands in the browser as raw text. Check the delivered source in the Network tab, not the template in your editor.

Stopping it coming back

Have the API return JSON for errors as well as successes, and check res.ok before parsing so an error page produces a clear message instead of a syntax error.

Uncaught SyntaxError: Unexpected token — common questions

Why does it say 'Unexpected token <' for an API call?
Because your code passed HTML to JSON.parse. The server answered with an error page — a 404, a 500, or a login redirect — rather than the JSON your code assumed it would always get.
What does 'Unexpected token export' or 'Cannot use import statement outside a module' mean?
The file uses ES module syntax but was loaded as a classic script. Add type="module" to the script tag, or let the bundler handle the file.
The error points at line 1 of a bundle. Where is the real problem?
A bundle is often one long line, so the position is meaningless without source maps. Enable them and the browser will show the original file and line.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor