日本語

Fixing React’s “Adjacent JSX elements must be wrapped in an enclosing tag” Error

​​React​Troubleshooting,TypeScript,Vite

If a React component returns two sibling elements, such as <h1> and <p>, the Vite dev server shows the error Adjacent JSX elements must be wrapped in an enclosing tag. Wrapping the sibling elements in one element, either a <> and </> Fragment or a <div>, clears the error. The TypeScript type checker reports the same mistake as JSX expressions must have one parent element.

This article covers the cause and the fix for the case where the page opened with npm run dev shows a black error overlay and the terminal shows Internal server error. I reproduced the error in my test environment and copied the messages shown in the browser and the terminal exactly as they appeared. I also checked the case where the error stays after the fix, and the messages from the type checker (tsc) and vite build.

Note

The test environment was Ubuntu 24.04 with Node.js 22.23.4. I opened a project created from the react-ts template of npm create vite@latest (react and react-dom 19.3.1, Vite 8.3.8, @vitejs/plugin-react 6.3.0, TypeScript 7.0.2) with the dev server (vite). The browser was Chromium from Playwright 1.63.0. I did not test Vite 7 or earlier, frameworks such as Next.js, or Windows and macOS.

Reproducing the error by returning two elements side by side

I deleted the extra template files (src/assets and src/App.css) and reduced src/index.css to one line with only spacing and font settings. Then I set src/App.tsx to the following. Inside return, <h1> and <p> sit side by side with no wrapping tag.

tsx
function App() {
  return (
    <h1>買い物メモ</h1>
    <p>牛乳を買う</p>
  )
}

export default App

I left src/main.tsx and vite.config.ts as the template generated them.

The message shown in the browser

When I started the dev server and opened the page, the page body stayed empty and the following error overlay appeared on top.

Vite error overlay in red text showing the message "Adjacent JSX elements must be wrapped in an enclosing tag," with App.tsx line 4 and the hint "Did you want a JSX fragment"
Browser error overlay after returning two elements side by side

The overlay showed the following text. I omitted the at lines of the stack trace.

text
[plugin:vite:oxc] Transform failed with 1 error:

[PARSE_ERROR] Adjacent JSX elements must be wrapped in an enclosing tag.
   ╭─[ src/App.tsx:4:5 ]
   │
 4 │     <p>牛乳を買う</p>
   │     ┬  
   │     ╰── 
   │ 
   │ Help: Did you want a JSX fragment `<>...</>`?
───╯
/path/to/app/src/App.tsx
Click outside, press Esc key, or fix the code to dismiss.
You can also disable this overlay by setting server.hmr.overlay to false in vite.config.ts.

/path/to/app is a placeholder for the folder where I placed the project during testing. The browser console also showed Failed to load resource: the server responded with a status of 500 (Internal Server Error).

The message means that adjacent JSX elements must be wrapped in an enclosing tag. The line below it, Help: Did you want a JSX fragment `<>...</>`?, suggests a JSX Fragment (<>...</>), and that is the fix. src/App.tsx:4:5 points to the position where the second element, <p>, starts.

The message shown in the terminal

The terminal running the dev server showed the same error as Internal server error. I omitted the at lines of the stack trace.

text
10:51:43 AM [vite] Internal server error: Transform failed with 1 error:

[PARSE_ERROR] Adjacent JSX elements must be wrapped in an enclosing tag.
   ╭─[ src/App.tsx:4:5 ]
   │
 4 │     <p>牛乳を買う</p>
   │     ┬  
   │     ╰── 
   │ 
   │ Help: Did you want a JSX fragment `<>...</>`?
───╯

  Plugin: vite:oxc
  File: /path/to/app/src/App.tsx

The same content also appeared as (client) Pre-transform error.

In my test, I deleted the dependency cache (node_modules/.vite) and then started the dev server while the error was still present. In that run, an error starting with (!) Failed to run dependency scan. Skipping dependency pre-bundling. appeared before the message above. When there is no cache, Vite scans the imports in the source code at startup to find the packages in use (dependency pre-bundling docs). This error came from that scan stopping because it could not read the same App.tsx. The underlying message was the same [PARSE_ERROR] Adjacent JSX elements must be wrapped in an enclosing tag.

As the [PARSE_ERROR] prefix shows, this is an error in the parsing stage, where the code is read. It stopped while Vite was transforming the file (Plugin: vite:oxc), so the inside of <div id="root"> on the page stayed empty. When I checked the installed packages, this message text was in Rolldown 1.2.19, which Vite 8.3.8 uses.

Why the elements must be wrapped in one element

The official React documentation, in its section on JSX syntax, Return a single root element, says that when a component returns multiple elements, they must be wrapped in a single parent tag.

The same page explains the reason. JSX looks like HTML, but it is compiled into JavaScript objects. A function cannot return two objects without wrapping them in something such as an array. For the same reason, two JSX tags cannot be returned unless they are wrapped in another tag or a Fragment.

Wrapping them in a Fragment (<> and </>) clears the error

I changed App.tsx so that the two elements are wrapped in <> and </>.

tsx
function App() {
  return (
    <>
      <h1>買い物メモ</h1>
      <p>牛乳を買う</p>
    </>
  )
}

export default App

After I reloaded the page, the error overlay disappeared and the heading and the sentence were displayed.

Page showing a heading reading "Kaimono Memo" (Japanese for shopping memo) and a line reading "Buy milk" (in Japanese)
Page after App.tsx was wrapped in <>…</> and the page was reloaded

The HTML inside <div id="root"> was <h1>買い物メモ</h1><p>牛乳を買う</p>. <> and </> add nothing to the HTML. The Fragment reference also states that grouping elements in a Fragment does not affect the DOM.

Point

When I opened the page while the error was present, saving the fixed file did not clear the error overlay. It did not change after 5 seconds, and it disappeared when I reloaded the page. In a different run, I opened a page that displayed correctly, saved a version with the two elements side by side, and then saved the fix. In that case, the error overlay disappeared without a reload. If the error stays after you fix the code, reload the page first.

When I fixed the code with the page still open, the terminal showed [vite] (client) hmr update /src/App.tsx, and the browser console showed [vite] hot updated: /src/App.tsx. When I fixed the code after opening a page that already showed the error, neither the terminal nor the console showed these messages.

The difference from wrapping in a div

Wrapping the elements in <div> instead of <> and </> also cleared the error.

tsx
function App() {
  return (
    <div>
      <h1>買い物メモ</h1>
      <p>牛乳を買う</p>
    </div>
  )
}

export default App

In this case, the HTML inside <div id="root"> was <div><h1>買い物メモ</h1><p>牛乳を買う</p></div>, with one extra <div>. The JSX syntax section also notes that <> and </> can be used when you do not want to add an extra <div>. Use <div> if you need an element for CSS layout, and use <> and </> if you want to keep the HTML structure unchanged.

The type checker and vite build also fail

When I ran the TypeScript type checker on the App.tsx from before the fix, it reported a different error message. The exit code was 1.

console
$ npx tsc -p tsconfig.app.json --noEmit
src/App.tsx(3,5): error TS2657: JSX expressions must have one parent element.

TS2657 means that a JSX expression needs one parent element. This error points to the first element, <h1>, on line 3.

npx vite build also stopped (exit code 1). I omitted the stack trace.

text
error during build:
Build failed with 1 error:

[builtin:vite-transform] Adjacent JSX elements must be wrapped in an enclosing tag.
   ╭─[ src/App.tsx:4:5 ]
   │
 4 │     <p>牛乳を買う</p>
   │     ┬  
   │     ╰── 
   │ 
   │ Help: Did you want a JSX fragment `<>...</>`?
───╯

The template's npm run build runs tsc -b && vite build. Before the fix, it stopped after printing only src/App.tsx(3,5): error TS2657: JSX expressions must have one parent element. and never reached vite build. After I wrapped the elements in <> and </>, tsc finished with no errors, and both vite build and npm run build finished with ✓ built in (all exit code 0).

​Posted by 86