Sentry Answers>Next.js>

TypeScript Error: Unexpected token `div`. Expected jsx identifier

TypeScript Error: Unexpected token `div`. Expected jsx identifier

Matthew C.

The ProblemJump To Solution

You are building a Next.js app and encounter the following error:

Click to Copy
Error: Unexpected token `div`. Expected jsx identifier

As an example, adding the following code to the /src/app/page.tsx file will cause the error:

Click to Copy
export default function Home() { const casesArr = [{ id: 1, name: "Case 1" }, { id: 2, name: "Case 2" }, { id: 3, name: "Case 3" }]; return ( <div> <ul> {casesArr.map((case) => ( <li key={case.id}>{case.name}</li> ))} </ul> </div> ); }

The Unexpected token error is a JavaScript syntax error. It occurs when a specific language construct is expected but not provided. In this case, TypeScript was expecting JSX to be returned but came across something unexpected.

The Solution

The issue may be due to a typo. It can also be due to using reserved JavaScript words, missing property values, extra or missing parentheses, or due to a trailing comma being in a place where an expression is expected.

Don’t Use Reserved JavaScript Words

To fix the code example above, change the name of the callback function argument in the map array method:

Click to Copy
export default function Home() { const casesArr = [{ id: 1, name: "Case 1" }, { id: 2, name: "Case 2" }, { id: 3, name: "Case 3" }]; return ( <div> <ul> {casesArr.map((caseItem) => ( <li key={caseItem.id}>{caseItem.name}</li> ))} </ul> </div> ); }

The word case is a reserved word in JavaScript. You can’t use reserved words as identifiers for variables, functions, classes, etc.

Missing Property Palue

The error can also be caused by a missing property value:

Click to Copy
<Image src="/next.svg" alt="logo" width={} height={200} />

Extra Parentheses or Missing Parentheses

An extra parenthesis can cause the issue:

Click to Copy
<Image src="/next.svg" alt="logo" width={20} height={200}} />

Missing parentheses can also cause an error:

Click to Copy
<Image src="/next.svg" alt="logo" width={20} height={200 />

Expression Expected

If you add a trailing comma where an expression is expected, such as after a map() function, you’ll get the error:

Click to Copy
<ul> {casesArr.map((caseItem) => ( <li key={caseItem.id}>{caseItem.name}</li> )),} </ul>

You can fix the error by removing the comma, or by adding an expression like:

Click to Copy
<ul> {casesArr.map((caseItem) => ( <li key={caseItem.id}>{caseItem.name}</li> )).reverse()} </ul>
  • Community SeriesDebug Next.js with Sentry
  • ResourcesJavaScript Frontend Error Monitoring 101
  • ResourcesSentry vs. Crashlytics: The Mobile Developer's Decision-Making Guide
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty Treats for Web Developers brought to you by Sentry. Web development tips and tricks hosted by Wes Bos and Scott Tolinski

    Listen to Syntax

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.