# The not-found.js Convention

> Specialized 404 boundaries that preserve your layout.

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

## The 404 boundary

The `not-found.js` file is used to render a custom UI when a user requests a resource that doesn't exist.

Just like `error.js` and `loading.js`, this file is nested *inside* the `layout.js` of its folder. This means your 'Page Not Found' screen will still beautifully render inside your application's shell, complete with sidebars and navigation.

## Beyond bad URLs

While it automatically catches users typing a bad URL, its most powerful use case is data fetching.

If a user visits `/users/123`, but user 123 was deleted from the database, this isn't a runtime *crash* (which belongs in `error.js`). It's a missing resource. You want to show a 404.

## The notFound() function

You create the UI by exporting a component from `not-found.js`.

To trigger it manually from a Server Component (like in the deleted user scenario), you import the `notFound` function from `next/navigation` and call it. `if (!user) notFound();`

## Common mistake: Bypassing error.js

A common point of confusion is how `notFound()` interacts with `error.js`. Since `notFound()` technically throws an error under the hood, shouldn't `error.js` catch it?

No. Next.js throws a highly specific `NEXT_NOT_FOUND` error. This error intentionally bypasses all `error.js` boundaries and searches only for the nearest `not-found.js` boundary.

## Tip: Granular 404s

Because `not-found.js` works at the segment level, you don't just have one global 404 page.

You can have a specific `not-found.js` inside `/dashboard` that says 'Dashboard widget not found', and another in `/blog` that says 'Article not found', each preserving their specific local layouts.

## Try it: The 404 Bypass

Watch the auto-demo as a bad URL request drops through the component tree, completely bypassing the ErrorBoundary and landing in the NotFound boundary.

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

## Check yourself

### Question 1

How do you manually trigger a not-found.js boundary when a database query returns null?

- A. throw new Error('404')
- B. return <NotFound />
- C. Call notFound() from next/navigation
- D. Set res.status(404)

Correct answer: C

Calling `notFound()` throws a special internal error that Next.js catches to render the nearest `not-found.js` boundary.

- Why not A: This will trigger error.js, not not-found.js.
- Why not B: While possible, it's not the Next.js idiomatic way to utilize the boundary.
- Why not D: There is no res object in modern App Router Server Components.

### Question 2

If you call notFound(), and the route has BOTH an error.js and a not-found.js, what happens?

- A. error.js catches it first because it's a thrown error
- B. not-found.js renders the UI
- C. Both render simultaneously
- D. The app crashes due to conflicting boundaries

Correct answer: B

The `notFound()` function throws a special internal error (`NEXT_NOT_FOUND`). Next.js recognizes this and intentionally bypasses `error.js` boundaries to find the nearest `not-found.js` boundary.

- Why not A: Next.js explicitly bypasses error.js for this specific call.
- Why not C: Only one boundary is triggered.
- Why not D: Next.js handles this routing gracefully.

## Remember this

- `not-found.js` renders a 404 UI *inside* the layout.
- Trigger it manually by calling `notFound()` from `next/navigation`.
- `notFound()` explicitly bypasses `error.js` boundaries.
- You can have different `not-found.js` files for different parts of your app.
