Matthew C.
—You are building a Next.js app and encounter the following error:
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:
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 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.
To fix the code example above, change the name of the callback function argument in the map
array method:
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.
The error can also be caused by a missing property value:
<Image src="/next.svg" alt="logo" width={} height={200} />
An extra parenthesis can cause the issue:
<Image src="/next.svg" alt="logo" width={20} height={200}} />
Missing parentheses can also cause an error:
<Image src="/next.svg" alt="logo" width={20} height={200 />
If you add a trailing comma where an expression is expected, such as after a map()
function, you’ll get the error:
<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:
<ul> {casesArr.map((caseItem) => ( <li key={caseItem.id}>{caseItem.name}</li> )).reverse()} </ul>
Tasty Treats for Web Developers brought to you by Sentry. Web development tips and tricks hosted by Wes Bos and Scott Tolinski
Listen to SyntaxLoved 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.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.