Next.js build fails on Windows with TypeError: Cannot read properties of null (reading 'useContext') - works fine on Vercel #82516
-
SummaryHey all, I’ve run into a frustrating issue where my Next.js app runs perfectly in dev mode and deploys fine to Vercel, but fails only when running Error log: Creating an optimized production build ...
Compiled successfully
Collecting page data
TypeError: Cannot read properties of null (reading 'useContext')
at p (C:\path\to\project\.next\server\pages\_error.js:8:45448)
Error occurred prerendering page "/404".
Export encountered an error on /_error: /404, exiting the build.What makes this odd:
What I’ve tried so far:
Environment:
Question: Any leads or workarounds would be greatly appreciated. I can prepare a minimal reproduction repo if that would help. Additional information
ExampleCreating an optimized production build ...
Compiled successfully
Collecting page data
TypeError: Cannot read properties of null (reading 'useContext')
at p (C:\path\to\project\.next\server\pages\_error.js:8:45448)
Error occurred prerendering page "/404".
Export encountered an error on /_error: /404, exiting the build. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hey @adiranawal, A few things that fixed it for me: Ensure client components have
|
Beta Was this translation helpful? Give feedback.
Hey @adiranawal,
I ran into a very similar issue when building on Windows, and in my case it turned out to be a combination of Next.js prerendering the
_errorpage and a dependency (Radix UI in particular) importinguseContextin a non-client context.A few things that fixed it for me:
Ensure client components have
"use client"Next.js 15+ is stricter about when hooks like
useContextcan run.If any of your components (or a dependency’s wrapper) calls hooks but is being rendered on the server, you’ll get this error.
Check:
For Radix UI components, you may need to wrap them in your own
"use client"file, then im…