SyntaxError: Unexpected token '<'
SyntaxError: Unexpected token 'export'
SyntaxError: Unexpected token '}' at position 42
JavaScript or JSON encountered a character it didnβt expect. Something is malformed.
Fix 1: HTML Instead of JSON
// β API returned HTML (404 page) instead of JSON
const res = await fetch('/api/data');
const data = await res.json(); // π₯ Unexpected token '<'
// β
Check the response first
const res = await fetch('/api/data');
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
Fix 2: Trailing Comma in JSON
// β JSON doesn't allow trailing commas
{
"name": "Alice",
"age": 30, // π₯ Trailing comma
}
// β
Remove the trailing comma
{
"name": "Alice",
"age": 30
}
Fix 3: ESM in CommonJS (or Vice Versa)
// β Using import in a CommonJS file
import express from 'express'; // π₯ Unexpected token 'import'
// β
Option A: Use require
const express = require('express');
// β
Option B: Add "type": "module" to package.json
Fix 4: Missing Bracket or Parenthesis
// β Missing closing bracket
function greet(name { // π₯ Missing )
return `Hello ${name}`;
}
// β
Fix the syntax
function greet(name) {
return `Hello ${name}`;
}
Fix 5: Comments in JSON
// β JSON doesn't support comments
{
// This is a comment π₯
"name": "Alice"
}
// β
Remove comments from JSON files
// Use .jsonc or .json5 if you need comments
Fix 6: Wrong File Being Served
# β Your bundler isn't running, so the browser gets raw source
# Check if your dev server is running
npm run dev
# Check if the build output exists
ls dist/
npm run build
Related resources
- Unexpected token in JSON fix
- Unexpected end of JSON input fix
- SyntaxError: import outside module fix
Related: JavaScript Array Methods Cheat Sheet Β· Assignment To Const Fix Β· Json Parse Error Fix