Next.js Discord

Discord Forum

How do I test the "global-error.js" page locally?

Unanswered
Bald Eagle posted this in #help-forum
Open in Discord
Avatar
Bald EagleOP
I tried throwing an error in the component and throwing an error on a click of a button.

I also tried running npm run build and npm run start instead of npm run dev

7 Replies

Avatar
Global-error.js only catches errors thrown in the RootLayout, throw an error in there and you’ll see it
Avatar
Bald EagleOP
For some reason it just displays the error in the console of the browser, but does not show the error.js or global-error.js page
Avatar
Need to be prod mode. You can use either a dynamic layout (export const revalidate = 0) or use react devtool to trigger an error
Avatar
Bald EagleOP
I thought that running npm run build and npm run start puts the app in prod mode
where would I need to add export const revalidate = 0?
Avatar
Bald EagleOP
Hey @joulev sorry to bother you, your tip with revalidate did help and I was able to see the error pages locally, but I was wondering about why this works?

In the docs it says that revalidate is used to purge the Data Cache, how does export const revalidate = 0 put you into Prod mode??

https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#revalidating-data
Avatar
It doesn’t put you into prod mode. It ensures that the code is not run during build so the error is caught during runtime by the boundary file rather than caught during build causing a build error.