Quick answer: The browser asked for a JavaScript or JSON file and the server answered with an HTML page, whose first character is <. Usually the URL is wrong and the host returned its 404 page or fell back to index.html. Open the Network tab, click the failing request and look at the response: you will see HTML. Fix the path or the server rule, and check response.ok before calling response.json() so the next wrong URL names itself.
The error points at line 1, column 1 of a file you know is valid, or at a JSON.parse call on a level file that opens fine in your editor. The file on your disk is not what the browser received. Somewhere between the request and the parser, an HTML document took its place. This guide shows the wording of the error in Chrome, Firefox and Safari, the hosting situations that swap HTML in for game files, how to confirm it in the Network tab, and the fixes for each.
- The exact error
- Why the token is “<”
- Where the HTML comes from
- Confirm it in the Network tab
- Fix: request the file where it is
- Fix: the server’s fallback rule and stale caches
- Fix: JSX and TypeScript need a build
- Fix: check the response before parsing it
- Confirm the fix
- What this error looks like in Bugnet
- Related guides
- Frequently asked questions
The exact error
For a script, Chrome prints:
Uncaught SyntaxError: Unexpected token '<' (at main.js:1:1)
For JSON loaded with fetch, it prints:
Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
Older Chrome versions worded the JSON case as Unexpected token < in JSON at position 0. The other browsers describe the same situation in their own terms, and a strict MIME check can replace the syntax error with a refusal to run the file at all. These are the messages from current builds:
| What received the HTML | Chrome, Edge | Firefox | Safari |
|---|---|---|---|
A classic <script src> | Uncaught SyntaxError: Unexpected token '<' | SyntaxError: expected expression, got '<' | SyntaxError: Unexpected token '<' |
JSON.parse() | SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON | SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data | SyntaxError: JSON Parse error: Unrecognized token '<' |
response.json() | Same as JSON.parse() | Same as JSON.parse() | SyntaxError: The string did not match the expected pattern. |
A module script or import() | Failed to load module script: Expected a JavaScript-or-Wasm module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec. | Loading module from “...” was blocked because of a disallowed MIME type (“text/html”). | TypeError: 'text/html' is not a valid JavaScript MIME type for module script '...'. |
A classic script, when the server sends X-Content-Type-Options: nosniff | Refused to execute script from '...' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled. | The resource from “...” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff). | Refused to execute ... as script because "X-Content-Type-Options: nosniff" was given and its Content-Type is not a script MIME type. |
All fifteen cells have one cause. Safari’s response.json() message is the least helpful of them, since it mentions neither JSON nor the character.
Two neighbors are worth recognizing. Unexpected end of JSON input means the body was empty. Unexpected token 'N', "Not Found" is not valid JSON means the server sent a plain-text error where you expected JSON. The diagnosis below is the same for both.
Why the token is “<”
An HTML document starts with <!DOCTYPE html> or <html>. A JavaScript parser handed that text stops at the first character, which is why the position is line 1, column 1. A JSON parser does the same at position 0.
The status code matters differently for the two kinds of request:
- Script tags. The browser does not run a script response with an error status. A real 404 gives you
Failed to load resource: the server responded with a status of 404and no syntax error. So when a script tag producesUnexpected token '<', the server answered 200 OK with an HTML body. Something on the host is turning unknown URLs into pages. fetch. A 404 or 500 does not reject the promise.response.json()parses whatever body arrived, so an HTML error page with any status ends in this error unless your code checks the status first.
Where the HTML comes from
- A fallback rule that serves
index.htmlfor every unknown path. Hosts set up for single-page apps, and some dev servers, answer any URL they cannot match with the app’s main page and status 200. A misspelled asset path therefore returns your own game page. - The wrong base path. The game is served from a subfolder (a GitHub project site, a portal’s file host) and the code asks for
/js/main.jsor/data/level1.jsonat the root of the domain. The guide to 404s on GitHub Pages and itch.io covers the path rules. - A file name that differs in case.
Level1.jsonon disk,level1.jsonin code. It loads on Windows and macOS and is a different file on a Linux host. - A stale page pointing at a bundle that no longer exists. Build tools put a hash in the file name, such as
main.4f3a1c.js. After a deploy, a cachedindex.htmlor a service worker still asks for the old name, and the fallback rule answers with HTML. Players see it and you do not, because your cache is fresh. - A page in front of your API. A leaderboard or save endpoint behind a login returns the sign-in page when the session has expired. A proxy or CDN returns its own HTML error page when your server is down.
- Source files served without a build step. JSX contains literal
<characters, and the browser cannot run it or TypeScript directly. The position in the message is the line of the first tag, not 1:1. - The wrong file in the tag. A
srcthat points at an.htmlfile or at a folder.
Confirm it in the Network tab
- Read the position in the message.
main.js:1:1means the whole file is something else. A position further in, in a file that contains JSX or type annotations, means cause 6. - Open the Network tab, tick Disable cache and reload. Click the request for the file named in the error.
- In Headers, read three things: the Request URL, the Status Code, and the
Content-Typeresponse header. For this error the content type istext/html. - Open the Response or Preview tab. If you see your own page, a fallback rule answered. If you see a login form or a “not found” page, the request went somewhere it should not, or the session expired.
- Compare the Request URL, character by character and including case, with where the file really is on the host. Paste it into a new tab if that is easier to read.
From a terminal, the headers alone are enough:
curl -I https://mygame.example/play/js/main.js
A content-type: text/html line in the output for a .js or .json URL confirms it.
Fix: request the file where it is
A path that begins with / starts at the root of the domain. A path without it starts at the folder of the page. With the game at https://user.github.io/my-game/:
<!-- Broken: asks for https://user.github.io/js/main.js -->
<script src="/js/main.js"></script>
<!-- Fixed: asks for https://user.github.io/my-game/js/main.js -->
<script src="js/main.js"></script>
The same applies in code:
// Broken on any host that serves the game from a subfolder
const level = await loadJson("/data/level1.json");
// Fixed: relative to the page
const level = await loadJson("data/level1.json");
If a bundler writes the tags for you, set its public base path. In Vite that is base: "./" in vite.config.js. Then make the case of every file name match the code exactly. A convention of all-lowercase asset names ends that class of bug.
Fix: the server’s fallback rule and stale caches
Stop answering asset URLs with a page
A fallback to index.html exists for apps with client-side routes such as /settings. A game that lives on one page does not need one. Remove it and a wrong URL becomes an honest 404 with the path in the console.
If the site does need the fallback, keep it away from asset folders. In nginx:
location / {
try_files $uri $uri/ /index.html;
}
location /assets/ {
try_files $uri =404;
}
location /js/ {
try_files $uri =404;
}
Hosted platforms have an equivalent setting for rewrites. Whatever the syntax, the goal is the same: a missing file under your asset paths returns 404, not a page.
Stale pages after a deploy
When only some players report the error and it starts right after a release, a cached page is asking for a bundle you deleted.
- Serve
index.htmlwithCache-Control: no-cacheso browsers revalidate it on every visit. The hashed bundles can be cached for as long as you like, because their names change when their contents do. - Keep the previous release’s bundles on the server for a while after each deploy. A tab that was open during the release can then finish loading the version it started.
- If the game registers a service worker, make sure it replaces its cached file list on update. To test without it, open Application > Service workers in Chrome DevTools and click Unregister, then Application > Storage > Clear site data. The update flow is covered in the guide to stale service worker caches.
Fix: JSX and TypeScript need a build
This file is valid JSX and invalid JavaScript:
// hud.jsx
const title = <h1>My Game</h1>;
Loaded directly, Chrome reports Unexpected token '<' at the position of the tag. A TypeScript file fails in the same way on its first annotation, with Unexpected token ':'. The script tag has to point at the compiled output. Run the build and deploy the output folder (dist by default in Vite), not the source folder:
npm run build
If the deployed index.html still contains a tag like <script type="module" src="/src/main.tsx">, you uploaded the source page.
Fix: check the response before parsing it
response.json() gives the parser whatever arrived. Checking first turns a confusing syntax error into a message that names the URL and the status:
async function loadJson(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`${url} returned HTTP ${response.status}`);
}
const type = response.headers.get("content-type") || "";
const text = await response.text();
if (type.includes("text/html") || text.trimStart().startsWith("<")) {
throw new Error(`${url} returned an HTML page, not JSON (final URL: ${response.url})`);
}
try {
return JSON.parse(text);
} catch (err) {
throw new Error(`${url} is not valid JSON: ${err.message}`);
}
}
response.url is the address after any redirects, which exposes a redirect to a login page. The function tests for HTML instead of requiring application/json because static hosts do not always label level formats with other file extensions as JSON.
Engine loaders call JSON.parse for you, so with Phaser, PixiJS or a similar loader the same failure shows the engine’s file in the stack. The cause and the Network tab check do not change.
Confirm the fix
- Reload with the cache disabled. In the Network tab, every
.jsrow should show a JavaScript content type and every.jsonrowapplication/json, all with status 200. - Request a file that does not exist, such as
js/nope.js, in a new tab. You want a 404, not your game page. - Test the deployed URL, not only the local one, and open it once in a private window to rule out a cached page.
- Check Firefox and Safari. Safari’s wording for the JSON case would otherwise be easy to mistake for a different bug.
What this error looks like in Bugnet
Both forms reach the Bugnet Web SDK. The script form is an uncaught error, and the JSON form is usually an unhandled promise rejection. Bugnet.init() listens for both and files each distinct error once per session, with the last 50 console lines, a screenshot of the canvas, the user agent and the game version. The report for a script names the file and line 1, and has no useful stack because nothing ran. Identical titles stack into one entry with an occurrence count, which helps with the stale-bundle case: a count that jumps after a deploy and then fades as caches expire is that cause.
The limits are the browser’s. The report does not contain the response body or its status, so it cannot say which page came back. The loadJson wrapper above fixes that, because its error message carries the URL and status and becomes the report title. And the SDK has to be running before the failure: if it is bundled into the same main.js that was answered with HTML, nothing reports. Load it with its own script tag ahead of the game bundle. Setup is in the SDK docs.
Related guides
- How to Fix a Web Game That Works Locally but Returns 404s on GitHub Pages or itch.io
- How to Fix “TypeError: Failed to fetch” in a Web Game
- How to Fix “Uncaught ReferenceError: is not defined” in a Web Game
- How to Fix “Failed to register a ServiceWorker” Errors in Web Games
- How to Fix “Unable to parse Build/WebGL.framework.js.gz” in a Unity WebGL Build
Frequently asked questions
What does Uncaught SyntaxError: Unexpected token '<' mean?
The file the browser tried to run or parse starts with the character <, which almost always means it received an HTML page in place of JavaScript or JSON. The usual source is a wrong URL that the host answered with its 404 page or with index.html.
Why does it say main.js:1 when my main.js is fine?
The browser is not looking at the file on your disk. It is reporting line 1 of the response it received for that URL, which is an HTML document. Click the request in the Network tab and open the Response tab to see what actually arrived.
What does Unexpected token '<', "<!DOCTYPE "... is not valid JSON mean?
JSON.parse or response.json() was given an HTML page. The fetch reached a URL that returned a page, such as a 404 page, a single-page-app fallback or a login page, and the code parsed the body without checking the status.
Why does this only happen for some players after I deploy an update?
Their browser or a service worker still has the old index.html, which asks for a hashed bundle name that no longer exists on the server. A fallback rule answers that request with HTML. Serve index.html with Cache-Control: no-cache and keep the previous bundles available for a while.
What are the Firefox and Safari versions of this error?
Firefox prints SyntaxError: expected expression, got '<' for scripts and JSON.parse: unexpected character at line 1 column 1 of the JSON data for JSON. Safari prints Unexpected token '<' for scripts, JSON Parse error: Unrecognized token '<' for JSON.parse, and The string did not match the expected pattern for response.json().
The parser is right and the URL is wrong: look at the response body in the Network tab before you look at your code. Then check response.ok before every response.json() so the next wrong path reports itself.