nextjs - 💡(How to fix) Fix TypeError: Cannot read properties of null (reading 'useState') when prerendering '/_not-found' on Vercel [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#86313Fetched 2026-04-08 02:11:32
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
0
Timeline (top)
closed ×1commented ×1labeled ×1locked ×1

Error Message

  1. On production build, Vercel fails: "Error occurred prerendering page '/_not-found'"
RAW_BUFFERClick to expand / collapse

Suggested issue text for Next.js

Title: TypeError: Cannot read properties of null (reading 'useState') when prerendering /_not-found on Vercel

Reproduction steps:

  1. Create an app/not-found.tsx (server component) and a small app dir.
  2. Deploy to Vercel with Next 16.0.3 (or with default Turbopack mode).
  3. On production build, Vercel fails: "Error occurred prerendering page '/_not-found'"
  4. Build logs include .next/server/chunks/* and references to next/dist/client/components/builtin/global-not-found.js

What I tried:

  • Pinning React & ReactDOM to 18.3.1
  • Clearing Vercel caches
  • Adding "use client" to app/not-found.tsx
  • Disabling Turbopack in Vercel build (TURBOPACK=0)
  • Running scripts/check-unwrapped-client-components.mjs (no unwrapped clients found)

Environment:

  • Next 15.5.6 -> 16.0.3 (attempted upgrade)
  • React 18.3.1
  • Vercel: Production build
  • Next config uses custom webpack alias setting (but env flags like NEXT_PRIVATE_WEBPACK_ONLY are set on Vercel)

Attachments:

  • Build trace from Vercel (clip the .vercel/output/diagnostics/trace file)
  • Full logs with stack traces

Please let me know if you want a repo I can give you access to for further debugging.

Related PR in our repo with reproduction and rollback: https://github.com/thekpsgroup/kps/pull/63 Potential hints:

  • The failure appears to happen when Next's built-in global-not-found or http-access-fallback module is included in prerender chunk
  • Something causes a client-only hook to be invoked in a server pre-render context

Please let me know if you want a repo I can give you access to for further debugging.

extent analysis

TL;DR

  • The issue can likely be resolved by ensuring that client-only hooks are not invoked in a server pre-render context, potentially by adding "use client" to the correct components or adjusting the Next.js configuration.

Guidance

  • Review the app/not-found.tsx component to ensure it does not inadvertently use client-side hooks in a server context.
  • Verify that the "use client" directive is correctly applied to components that use client-side hooks like useState.
  • Check the custom webpack alias settings in the Next.js configuration to ensure they do not interfere with the prerendering process.
  • Consider temporarily disabling Turbopack or adjusting its configuration to see if it affects the prerendering issue.

Example

// Example of adding "use client" to a component
'use client';
import { useState } from 'react';

export default function MyComponent() {
  const [state, setState] = useState(null);
  // Component code here
}

Notes

  • The exact solution may depend on the specifics of the app/not-found.tsx component and the custom Next.js configuration.
  • The issue seems related to the inclusion of client-only hooks in server pre-render contexts, but the root cause could be nuanced.

Recommendation

  • Apply workaround: Adjust the components and configuration to ensure client-only hooks are not invoked during server pre-rendering, as this approach directly addresses the likely cause of the error without requiring an upgrade or significant changes to the environment.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING