# The global-error.js Convention

> The absolute last line of defense for root layout crashes.

Subject: Next.js | Lesson 11 of 65 | 4 min read
Interactive lesson: https://codewithanimation.in/nextjs/global-error-file
Previous: [The error.js Convention](https://codewithanimation.in/nextjs/error-file.md) | Next: [The not-found.js Convention](https://codewithanimation.in/nextjs/not-found-file.md)

## The root crash problem

We know that an `error.js` file catches errors in its segment, but because it is nested *inside* the layout of the same folder, it cannot catch errors thrown by that layout.

So, what happens if your absolute Root Layout (`app/layout.tsx`) crashes? A standard `app/error.tsx` cannot catch it. Without intervention, your entire application goes down.

This is exactly what `global-error.js` is designed to solve.

## Total replacement

`global-error.js` sits at the absolute top of the component tree, wrapping even the Root Layout.

When a catastrophic error occurs in the Root Layout (e.g., your global theme provider throws an error, or the root data fetch fails), `global-error.js` completely replaces the Root Layout to show a fallback UI.

## Mandatory HTML tags

Because `global-error.js` physically replaces the Root Layout, it is responsible for rendering the foundational HTML document.

Therefore, a `global-error.js` component **must** define its own `<html>` and `<body>` tags. Like all error boundaries, it must also include the `'use client'` directive.

## Common mistake: Development mode confusion

The biggest frustration developers face with `global-error.js` is trying to test it locally.

In development mode, Next.js will intercept the crash and display its own development error overlay, completely ignoring your custom `global-error.js` UI. You typically have to run a production build (`npm run build && npm run start`) to actually see it working.

## Tip: Last resort only

Do not use `global-error.js` as your primary error handling strategy.

It should only be used as a last resort for catastrophic root-level failures. For all other routes, use standard `error.js` files so that the user's layout and navigation remain intact.

## Try it: The Component Hierarchy

Study the nesting diagram below. Notice how the standard ErrorBoundary sits inside the RootLayout, making it impossible to catch a root crash. Only the GlobalError sits outside everything.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/global-error-file

## Check yourself

### Question 1

Unlike a standard error.js, what MUST a global-error.js component return?

- A. A standard React Fragment (<>...</>)
- B. Its own <html> and <body> tags
- C. A <Suspense> boundary
- D. A redirect to the homepage

Correct answer: B

Because `global-error.js` completely replaces the Root Layout upon failure, it takes on the responsibility of rendering the `<html>` and `<body>` tags so the browser has a valid document.

- Why not A: It needs to render the actual document wrapper.
- Why not C: Suspense is for loading, not error boundaries.
- Why not D: It returns a fallback UI, not a redirect.

### Question 2

Why can't you just put a standard error.js in the app/ folder to catch Root Layout errors?

- A. Because error.js only works on Server Components
- B. Because the app/ folder doesn't allow error.js files
- C. Because a standard error.js nests inside the layout of the same folder
- D. Because Root Layouts are immune to crashing

Correct answer: C

Next.js nests `error.js` inside the layout of the same directory. An `app/error.js` cannot catch an error in `app/layout.js` because the error occurs "above" the boundary in the React tree.

- Why not A: error.js must be a Client Component.
- Why not B: You can put an error.js in the app/ folder, it just catches errors in page.js.
- Why not D: They can absolutely crash.

## Remember this

- `global-error.js` catches errors in the Root Layout.
- It completely replaces the Root Layout when active.
- It MUST define its own `<html>` and `<body>` tags.
- It is hard to test in dev mode (Next.js shows an overlay instead).
