Create a Custom Error Page in Next.js

Last Updated : 11 Jul, 2026

Creating a custom error page in Next.js allows you to provide a better user experience by displaying a customized page whenever users visit a route that does not exist.

Custom error page

A 404 (Not Found) page is displayed when a user tries to access a page that does not exist in the application. Instead of showing the default error page, Next.js allows you to create a custom page that matches your application's design.

default404page

Approach

To create a custom 404 page in Next.js, create a not-found.js file inside the app directory. Whenever a user visits a route that does not exist, Next.js automatically renders this file.

// File Path: src/app/not-found.js

export default function NotFound() {
return <>YOUR COMPONENT HERE</>;
}

Steps to Create a Custom Error Page in Next.js

Prerequisite: Before following this tutorial, make sure you have already created a Next.js project. If not, refer to the Next.js Installation and First Application article.

Step 1: The project structure will look like the following.

Project Structure:

creating a 404,js file

Step 2: Create the Custom Error Page

Create a new file named not-found.js inside the src/app directory and add the following code.

JavaScript
// File Path: src/app/not-found.js
export default function NotFound() {
    return (
        <main
            style={{
                textAlign: "center",
                padding: "60px",
            }}
        >
            <h1>
                Welcome to{" "}
                <span style={{ color: "green" }}>
                    GeeksforGeeks
                </span>
            </h1>
            <h2>
                404 - Page Not Found
            </h2>
            <p>
                Sorry, the page you are looking for does not exist.
            </p>
            <p>
                Please check the URL and try again.
            </p>
        </main>
    );
}

Explanation: In the above example, we create a not-found.js file inside the app directory. Whenever a user visits a route that does not exist, Next.js automatically renders this page instead of displaying the default 404 page. This allows you to customize the appearance and message shown to users.


Step 3: Run the application

npm run dev 

Output: And now let us go to a non-existing page on the website to encounter the 404 error.

Custom 404 page (created using the above code)
Comment

Explore