πŸ”§ Error Fixes
Β· 1 min read
Last updated on

Fix: SyntaxError β€” Unexpected token


SyntaxError: Unexpected token '}'
SyntaxError: Unexpected token '<'
SyntaxError: Unexpected token 'export'

JavaScript encountered a character it didn’t expect. The specific token tells you what went wrong.

Unexpected token } or ) β€” mismatched brackets

// ❌ Extra closing bracket
function greet(name) {
  if (name) {
    console.log(name);
  }}  // Extra }
}

// βœ… Match your brackets
function greet(name) {
  if (name) {
    console.log(name);
  }
}

Use your editor’s bracket matching feature (VS Code highlights matching brackets).

Unexpected token < β€” HTML instead of JS

Your server is returning an HTML page (like a 404) instead of JavaScript.

// ❌ The API returned HTML (error page) instead of JSON
const data = await response.json();
// SyntaxError: Unexpected token '<' (the HTML starts with <html>)

// βœ… Check the response first
const response = await fetch('/api/data');
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();

Also check that your script src paths are correct in HTML.

Unexpected token export β€” module system mismatch

// ❌ Using ES modules in a CommonJS file
export default function greet() {}
// SyntaxError: Unexpected token 'export'

Fixes:

  • Add "type": "module" to package.json
  • Or rename the file to .mjs
  • Or use module.exports instead of export

Unexpected token in JSON

// ❌ Trailing comma in JSON
{ "name": "Alice", }  // SyntaxError!

// ❌ Single quotes in JSON
{ 'name': 'Alice' }   // SyntaxError!

// βœ… Valid JSON
{ "name": "Alice" }

See also: Unexpected token in JSON fix for the full guide.

Quick debugging

The error message includes a line number. Go to that line and look for:

  • Missing or extra brackets {}, (), []
  • Missing commas or semicolons
  • Wrong quotes (single vs. double in JSON)
  • Copy-pasted invisible characters (retype the line)

See also: ESLint parsing error fix

Related: JavaScript Array Methods Cheat Sheet