/ ← JavaScript Checker Fixes / Syntax Errors
In this guide
  1. Why one syntax error breaks a whole file
  2. What each message is telling you
  3. The most common cause: the file is not JavaScript
  4. Modern syntax and older browsers
  5. The fix, step by step
  6. Pasted snippets, curly quotes and invisible characters
  7. A pre-release check that catches syntax errors
  8. Edge cases
  9. Questions and answers

Why one syntax error breaks a whole file

Before running a script, the browser parses all of it to understand its structure. If the parser meets something that is not valid JavaScript, it stops, reports a SyntaxError, and discards the file. Not one line of it runs, including the lines above the mistake. That makes a syntax error very different from a runtime error, which only interrupts the piece of work that was under way.

The damage depends on how your scripts are packaged. On a site that bundles everything into one file, a single stray character removes every feature. On a site with many small files, only the features in the broken file disappear, which can make the problem harder to spot: the page looks fine until someone tries the one thing that no longer works.

JSON is a special case. JSON.parse also throws a SyntaxError when it is given text that is not valid JSON, but that happens while the code is running, so only the code waiting for that data fails. The fix for those is usually on the server, as explained below.

πŸ’‘ Syntax errors are the one kind of JavaScript problem you can catch completely before release. Every one that reaches the live site is a sign that a check is missing from the way code gets published.

What each message is telling you

MessageMost likely causeWhere to look
Unexpected token '<' at line 1, column 1The server sent HTML, usually a 404 page, a login page or a maintenance page, instead of the script.The Network panel response for that file, not the code.
Unexpected token '}' or ')'A bracket closed that was never opened, often after an edit that removed a line.The lines just before the reported position.
Unexpected end of inputA bracket, brace or string opened and never closed, or a file cut short during upload.The end of the file, and its size compared with the original.
Invalid or unexpected tokenA character JavaScript does not accept, such as curly quotes or an invisible character pasted from a document.Quotes and spaces in recently pasted code.
missing ) after argument listA missing comma between arguments, or a quote inside a string that ends it early.The function call on the reported line.
Cannot use import statement outside a moduleA file using import loaded as a classic script.The script tag's type attribute.
Unexpected token '?' or '.' in an older browserModern syntax such as ?. or ?? sent to a browser too old to understand it.Your build target and the visitor's browser version.
Unexpected token 'export'Module code served to a browser as a classic script, or a Node-only file shipped to the browser.How the file is loaded and which build output is being used.
Unexpected token '<<' or '==' at the start of a lineUnresolved Git merge markers left in the file.Search the file for <<<<<<<.

Firefox and Safari word some of these differently. Firefox, for example, reports expected expression, got '}' and unterminated string literal. The position is what matters: every browser points at the place where the parser gave up, which is at or just after the real mistake.

The most common cause: the file is not JavaScript

A syntax error at line 1, column 1 almost never means the code is wrong. It means the browser received something other than code. Check what the server actually sent before opening an editor.

curl -sI https://example.com/assets/app.4c2e.js
# Look for: HTTP/2 200 and content-type: application/javascript (or text/javascript)

curl -s https://example.com/assets/app.4c2e.js | head -c 300
# If this starts with <!DOCTYPE html> the server returned a web page, not a script

Typical reasons the server returns a page instead of a script:

The same pattern shows up with data. Unexpected token '<', "<!DOCTYPE "... is not valid JSON means code asked an API for JSON and received an HTML error page. Fix the endpoint or the address, and make the calling code check the response status before parsing.

⚠️ Configure your server so that missing files under your assets folder return a real 404 status, not your home page. A clear 404 in the Network panel is far easier to diagnose than a misleading syntax error.

Modern syntax and older browsers

JavaScript gains new syntax every year. Code using optional chaining, nullish coalescing, class fields or top-level await is perfectly valid, but a browser released before that feature arrived treats it as a syntax error. The usual victims are older iPhones and iPads that cannot update Safari, older smart TVs, in-app browsers and locked-down corporate machines.

The fix is to decide which browsers you support and let the build tool compile down to that level. Most tools read the same shared setting, a Browserslist query, either in package.json or a .browserslistrc file:

{
  "browserslist": [
    "> 0.5% in GB",
    "last 2 versions",
    "not dead",
    "iOS >= 15"
  ]
}

Babel, SWC and Autoprefixer read this directly. Some bundlers use their own setting instead: esbuild takes a --target list such as es2020,safari15, and Vite uses build.target. Whatever the tool, check what it actually outputs by searching the built file for ?. or ?? after changing the target.

Two points catch people out. First, compiling handles syntax but not missing functions: a browser without structuredClone still needs a polyfill, and that failure is a runtime error, not a syntax error. Second, many build set-ups skip compiling files in node_modules, so a dependency that ships modern syntax can slip through untouched. Check your build tool's documentation for how to include specific packages in compilation.

The fix, step by step

Step 1

Read the exact message and position

Take the syntax errors reported by the JavaScript Checker, then open the page in developer tools and click the file link beside each SyntaxError. Note the file, line and column, and the exact wording. A position of line 1, column 1 points at the response rather than the code. Any other position points at a real mistake at or just before that spot, so read the few lines above it as well.

Step 2

Confirm the file really is JavaScript

Open the file in the Network panel and look at the status, the Content-Type header and the first characters of the response. If you see HTML, the problem is a missing file, a cached page asking for an old bundle name, a login or challenge page, or a wrong path. Fix the server, purge caches and make missing assets return a real 404 rather than editing any code.

Step 3

Repair the broken source, not the bundle

Use a source map or your file naming to find the original source file, and fix the mistake there. Look for unclosed brackets or strings, missing commas, stray merge markers and lines half-deleted during an edit. Your editor's bracket matching and syntax colouring make these obvious. Never patch a minified file on the server by hand, because the next build will silently overwrite the repair.

Step 4

Match import syntax to how the script loads

A file that uses import or export must be loaded with type="module", or bundled into a classic script by your build tool. If you add the module type, remember module scripts are deferred and have their own scope, so code that relied on global variables may need updating. Do not ship CommonJS files that use require straight to the browser; bundle them first.

Step 5

Compile for the browsers you support

Write down the oldest browsers your visitors genuinely use, using your analytics, and express that as a Browserslist query or your bundler's target setting. Rebuild, then search the output for modern syntax such as ?. to confirm it was compiled. If a dependency still ships modern syntax, add it to the list of packages your build compiles, or replace it with one that supports your targets.

Step 6

Clean up snippets pasted into your CMS

Open any tracking, chat or custom code box in your CMS, theme settings or tag manager and look for curly quotes, invisible characters and code cut off mid-line. Retype the quotes as straight quotes in a plain text editor, remove stray HTML tags inside script boxes, and paste the snippet again from the vendor's original instructions. Then reload the page and check the console is clear.

Step 7

Parse-check every build before it ships

Add ESLint to the project so syntax errors show in the editor as you type, and run it in your deployment pipeline so a failing file stops the release. After the build step, run a quick parse check over the output files. Finally, re-run the JavaScript Checker on the live pages you fixed to confirm the syntax errors have gone and the features in those files now work.

Pasted snippets, curly quotes and invisible characters

On WordPress, Shopify and similar platforms, many syntax errors are not written by a developer at all. They arrive when someone copies a tracking snippet from an email, a PDF or a word processor and pastes it into a settings box.

// Broken: typographic quotes from a document
gtag(β€˜config’, β€˜G-XXXXXXX’);

// Fixed: straight quotes
gtag('config', 'G-XXXXXXX');

A pre-release check that catches syntax errors

Your build tool already parses every file it bundles, so a clean build is itself a syntax check for your own code. The gaps are files that skip the build: hand-edited scripts in a theme folder, files copied from a vendor, and inline scripts in templates. A few lines in your deployment pipeline close those gaps.

# Fail the release if any shipped script does not parse
for f in dist/assets/*.js public/js/*.js; do
  node --check "$f" || exit 1
done

# Lint the source as well
npx eslint "src/**/*.js"

node --check parses a file without running it. It treats .js files according to the nearest package.json, so files using import may need the .mjs extension or a package type of module to be checked correctly. For inline scripts in HTML templates, the simplest safeguard is to move them into separate files where these checks can reach them.

πŸ’‘ Merge markers are a common surprise after a rushed conflict resolution. A pre-commit hook that rejects any file containing a line starting with seven less-than signs costs nothing and stops them for good.

Edge cases

Questions and answers

1. Why does one syntax error stop the whole script?

The browser has to understand the entire file before running any of it. When the parser finds something that is not valid JavaScript, it cannot know what the rest of the file was meant to do, so it reports the error and runs nothing from that file, including the lines above the mistake. That is why a single missing bracket in a bundled file can disable every feature it contains, even ones that have nothing to do with the broken line.

2. What does Unexpected token less-than at line 1 mean?

It almost always means the browser asked for a script and received an HTML page instead, and the first character of that page, the opening angle bracket of the doctype, is not valid JavaScript. The page is usually a 404, a login or maintenance page, or a bot challenge. Look at the file's response in the Network panel. Fixing the path, purging stale cached HTML after a deploy, or making missing assets return a real 404 solves it.

3. How do I fix Cannot use import statement outside a module?

The file uses import or export but the browser loaded it as an ordinary script. Either add type equals module to the script tag, or let your bundler combine the modules into a single classic script. If you switch to a module script, note that it runs deferred and keeps its variables to itself, so other scripts that expected its functions to be global may need updating to import them or to read them from a shared object.

4. Why does my site work in Chrome but break on an old iPhone?

Newer syntax such as optional chaining, nullish coalescing or class fields is a syntax error in browsers released before those features existed, and older iPhones and iPads that cannot update their operating system are stuck on older Safari versions. Set a Browserslist query or your bundler's target to include the oldest browsers your analytics shows real visitors using, rebuild, and check that dependencies are compiled too, since many set-ups skip them by default.

5. Can pasting code into WordPress cause syntax errors?

Yes, and it is one of the most common causes on WordPress sites. Copying a snippet from an email, PDF or document can turn straight quotes into curly ones, add invisible characters, or cut the code short. Pasting into a visual editor can wrap it in paragraph tags. Paste snippets into a code block or a dedicated header and footer setting, copy them from the vendor's own instructions, and check the console after saving.

6. Is a JSON parse error a JavaScript syntax error?

It is reported as a SyntaxError, but it happens while code is running rather than when the file is loaded, so only the code waiting for that data fails. The usual cause is an API or endpoint returning an HTML error page, or an empty response, instead of JSON. Check the request in the Network panel, fix the server or the address, and make the calling code check the response status before parsing it.

7. Will a linter catch every syntax error?

A linter such as ESLint catches every syntax error in the files it is pointed at, which is why running it in the editor and in your deployment pipeline is so effective. It will not catch problems that happen after the code leaves your hands, such as a server returning an HTML page for a script, a CDN serving a truncated file, or a snippet pasted into a CMS field. Checking the live page after each release covers those.

Find the scripts that fail to run

The JavaScript Checker loads your page in headless Chromium and captures console errors and warnings, including syntax errors, and lists every script with its size and parse and execution time.

Open JavaScript Checker Fixes β†’
πŸ’¬ Got a problem?
Related Guides: All JavaScript Checker Fixes  ·  Fixing Console Errors at Page Load  ·  JavaScript Issues in WordPress  ·  JS Checker Guide  ·  Learning Hub

About aiwebpageseo

aiwebpageseo.com is a data-driven SEO and AEO (Answer Engine Optimisation) platform providing a free suite of technical SEO tools. Rather than relying on AI-theorised assumptions, the platform analyses live URL performance, delivering objective diagnostics, page speed metrics, CLS debugging, and site crawl data alongside actionable technical tutorials.