Next.js shows an error page saying the root layout has no <html> or <body>
Fix it fast
Most common cause, 1 of 3app/layout returns children without the document tags
Return the two tags from the top layout and put your wrapper inside body.
// app/layout.tsx
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<main>{children}</main>
</body>
</html>
)
}Short answer
In the App Router, any layout.js with no layout above it is a root layout, and every root layout has to return the <html> and <body> elements itself. The error appears when app/layout.js returns only a wrapper or a fragment, or when a route group has its own top layout without them. Add both tags to every root layout.
Which cause do you have?
List every layout file and work out which ones have no layout above them. Those are the root layouts the rule applies to.
find app -name 'layout.*' | sortThen find the line you see in this list and open its fix:
- 1app/layout returns children without the document tags
Every route shows the error, and app/layout.js returns a div, a main element or a fragment - 2A route group has its own root layout without the tags
Only routes inside one group, such as /cart under app/(shop), show the error - 3The html element is returned only under some condition
The error comes and goes with a locale, a feature flag or an auth state
What does the error look like?
Missing <html> and <body> tags in the root layout.One route works while another route of the same app shows the error overlay
Cause 1 of 3
app/layout returns children without the document tags
You seeEvery route shows the error, and app/layout.js returns a div, a main element or a fragment
Check
Open app/layout.js (or .tsx). Next.js does not add <html> or <body> for you, so a layout that only wraps children in some element leaves the document without them.
grep -nE '<html|<body' app/layout.*Fix
Return the two tags from the top layout and put your wrapper inside body. Page titles and meta tags belong in the metadata export, not in a hand-written head.
// app/layout.tsx
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<main>{children}</main>
</body>
</html>
)
}Verify
Restart next dev and open the home page. The overlay is gone and the page source starts with <html lang="en">.
curl -s http://localhost:3000/ | head -c 200Confirmed by nextjs.org
Cause 2 of 3
A route group has its own root layout without the tags
You seeOnly routes inside one group, such as /cart under app/(shop), show the error
Check
When there is no app/layout.js, each group's top layout becomes a separate root layout. One group may have html and body while another group's layout returns only a section.
ls app/layout.* 2>/dev/null || echo 'no top-level layout'
grep -LE '<html' app/\(*\)/layout.*Fix
Give every group-level root layout its own html and body, or add one shared app/layout.js and turn the group layouts into ordinary nested layouts without the document tags.
// app/(shop)/layout.tsx
export default function ShopLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body><section>{children}</section></body>
</html>
)
}Verify
Open a route from each group. All of them render; moving between groups that use separate root layouts does a full page load, which is expected.
curl -s http://localhost:3000/cart | grep -c 'Missing <html>'Confirmed by nextjs.org
Cause 3 of 3
The html element is returned only under some condition
You seeThe error comes and goes with a locale, a feature flag or an auth state
Check
Look for an early return, a ternary or a provider in the root layout that sometimes returns children without the document tags, for example while a locale is still unknown.
grep -nE 'return|\?' app/layout.* app/*/layout.*Fix
Always return html and body from the root layout, and put conditions inside body. A value such as the lang attribute can change; the two elements themselves must always be there.
export default async function LocaleLayout({ children, params }) {
const { locale } = await params
return (
<html lang={locale ?? 'en'}>
<body>{children}</body>
</html>
)
}Verify
Load the route in each state that used to fail (each locale, signed in and signed out).
npx next buildConfirmed by nextjs.org
How do I stop it coming back?
- Keep one file as the only place that renders html and body, unless you really need separate root layouts.
- When you add a route group with its own top layout, add the document tags in the same commit.
- Use the Metadata API for head content so nobody is tempted to build the document by hand.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.