In this article, we cover how to create an error-handling page when any error occurs on Next.js
In NEXT.JS, It provides a custom error-handling page,
- Create
error.js: Inside the pages directory of your Next.js project, create a file namederror.js. This file will handle all errors that occur in your application. - The name must be the same as given above, and it must be in the “app” root directory.
- Define Custom Error Component: In this file, you can add UI and other functions that you want to display when an error occurs
- Whenever an error occurs on any page, this page will be displayed on that place
With this, we can handle errors using custom error pages on NEXT.JS