nextjs - 💡(How to fix) Fix Build fails with 'Cannot read properties of null (reading useContext)' error during /_global-error prerendering (v16.0.2, v16.0.3) [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#86178Fetched 2026-04-08 02:12:10
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
0
Timeline (top)
closed ×1commented ×1labeled ×1locked ×1

Error Message

TypeError: Cannot read properties of null (reading 'useContext')
Error occurred prerendering page "/_global-error"
Digest: 2497494197 (v16.0.3)
Digest: 517458545 (v16.0.2)

Fix Action

Fix / Workaround

  • Cannot upgrade to Next.js 16
  • Cannot build for production with Next.js 16.0.x
  • Blocks React 19 adoption (requires Next.js 16)
  • No workaround available

Code Example

TypeError: Cannot read properties of null (reading 'useContext')
Error occurred prerendering page "/_global-error"
Digest: 2497494197 (v16.0.3)
Digest: 517458545 (v16.0.2)

---

Creating an optimized production build ...
Compiled successfully in 79s
Skipping validation of types
Collecting page data using 11 workers ...
Generating static pages using 11 workers (0/62) ...
Generating static pages using 11 workers (15/62) 
Generating static pages using 11 workers (30/62) 
Generating static pages using 11 workers (46/62) 

Error occurred prerendering page "/_global-error". Read more: https://nextjs.org/docs/messages/prerender-error
TypeError: Cannot read properties of null (reading 'useContext')
    at ignore-listed frames {
  digest: '2497494197'
}
Export encountered an error on /_global-error/page: /_global-error, exiting the build.
 Next.js build worker exited with code: 1 and signal: null

---

'use client'

export const dynamic = 'force-dynamic'

type GlobalErrorProps = {
  error: Error & { digest?: string }
  reset: () => void
}

export default function GlobalError({ error, reset }: GlobalErrorProps) {
  return (
    <html lang="en">
      <body>
        <div>
          <h2>Something went wrong!</h2>
          <p>An error occurred.</p>
          <button type="button" onClick={() => reset()}>
            Try again
          </button>
        </div>
      </body>
    </html>
  )
}
RAW_BUFFERClick to expand / collapse

Bug Description

Production builds fail during static page generation of /_global-error with a useContext null reference error. This error occurs in Next.js 16's internal error page prerendering system, even when no custom global-error.tsx file exists.

Error Message

TypeError: Cannot read properties of null (reading 'useContext')
Error occurred prerendering page "/_global-error"
Digest: 2497494197 (v16.0.3)
Digest: 517458545 (v16.0.2)

Environment

  • Next.js Versions Tested: 16.0.3, 16.0.2
  • React Version: 19.2.0
  • Node Version: Latest LTS
  • Platform: macOS (Darwin 24.6.0)
  • Package Manager: npm
  • Build Command: npm run build / next build
  • Router: App Router
  • Upgrade Method: npx @next/codemod@canary upgrade latest

Reproduction Steps

  1. Upgrade from Next.js 15.5.6 to 16.0.3 using automated upgrade CLI
  2. Apply recommended codemods (remove-experimental-ppr, remove-unstable-prefix, middleware-to-proxy)
  3. Upgrade React to 19.2.0
  4. Run npm run build
  5. Build fails during "Generating static pages" when prerendering /_global-error

Expected Behavior

Build should complete successfully and generate error pages.

Actual Behavior

Build fails with useContext null reference error during error page prerendering at /_global-error.

Investigation Summary

CRITICAL FINDING: Error occurs even with global-error.tsx completely removed from the project.

This proves the bug is in Next.js 16's internal error handling system, not in application code.

Attempted Fixes (All Failed)

  1. Removed useEffect hook from global-error.tsx
  2. Removed all client-side logging
  3. Added export const dynamic = 'force-dynamic' to global-error.tsx
  4. Removed CSS module imports
  5. Used inline styles only
  6. Deleted global-error.tsx entirely - Error still occurs
  7. Clean cache rebuilds (rm -rf .next)
  8. Disabled all providers
  9. Minimal component structure with no hooks

Error persists even with:

  • ✅ No custom global-error.tsx file (using Next.js internal default)
  • ✅ No providers (SessionProvider, Rollbar, etc.)
  • ✅ No hooks (no useEffect, useState, etc.)
  • ✅ No imports beyond React
  • ✅ Minimal component structure
  • ✅ Fresh .next directory

Versions Tested

VersionHtml ErroruseContext ErrorBuild Status
15.5.6❌ PresentN/A❌ BLOCKED
16.0.2✅ Fixed❌ Present❌ BLOCKED
16.0.3✅ Fixed❌ Present❌ BLOCKED

Build Output

Creating an optimized production build ...
✓ Compiled successfully in 79s
Skipping validation of types
Collecting page data using 11 workers ...
Generating static pages using 11 workers (0/62) ...
Generating static pages using 11 workers (15/62) 
Generating static pages using 11 workers (30/62) 
Generating static pages using 11 workers (46/62) 

Error occurred prerendering page "/_global-error". Read more: https://nextjs.org/docs/messages/prerender-error
TypeError: Cannot read properties of null (reading 'useContext')
    at ignore-listed frames {
  digest: '2497494197'
}
Export encountered an error on /_global-error/page: /_global-error, exiting the build.
⨯ Next.js build worker exited with code: 1 and signal: null

Minimal global-error.tsx That Still Fails

'use client'

export const dynamic = 'force-dynamic'

type GlobalErrorProps = {
  error: Error & { digest?: string }
  reset: () => void
}

export default function GlobalError({ error, reset }: GlobalErrorProps) {
  return (
    <html lang="en">
      <body>
        <div>
          <h2>Something went wrong!</h2>
          <p>An error occurred.</p>
          <button type="button" onClick={() => reset()}>
            Try again
          </button>
        </div>
      </body>
    </html>
  )
}

BUT: Deleting this file entirely (letting Next.js use its internal default) produces the same error, confirming this is a Next.js framework bug.

Impact

Severity: CRITICAL - Blocks Next.js 16 adoption

  • Cannot upgrade to Next.js 16
  • Cannot build for production with Next.js 16.0.x
  • Blocks React 19 adoption (requires Next.js 16)
  • No workaround available

Related Issues

This appears to be related to:

  • Error page prerendering in App Router
  • Internal React context handling during static generation
  • Next.js 16's changes to error boundary rendering

Upgrade Context

Upgrading from Next.js 15.5.6 which has a different critical bug (#86177). Next.js 16 fixes that Html import error but introduces this new useContext error in its internal error handling system.

Request

Please investigate the internal /_global-error page prerendering in Next.js 16.0.x. The useContext null reference is occurring during static generation of Next.js's auto-generated internal error page, making it impossible to work around from application code.

The error occurs even when:

  1. No custom global-error.tsx exists
  2. The component has no hooks
  3. Dynamic rendering is forced
  4. All providers are disabled

This suggests a bug in Next.js 16's core error page rendering system.

extent analysis

TL;DR

The most likely fix for the useContext null reference error during static page generation of /_global-error in Next.js 16 is to wait for an official patch or fix from the Next.js team, as the issue is related to the internal error handling system.

Guidance

  • Verify that the issue persists even when using the minimal global-error.tsx file provided in the issue description, and when deleting the file entirely to use Next.js's internal default.
  • Check the Next.js GitHub repository for any open issues or pull requests related to the internal error handling system and useContext null reference errors.
  • Consider downgrading to Next.js 15.5.6, despite its own critical bug, as a temporary workaround until the issue is resolved in Next.js 16.
  • Monitor the Next.js release notes and changelog for any updates or fixes related to the internal error handling system.

Example

No code example is provided, as the issue is related to the internal Next.js error handling system and not a specific code snippet.

Notes

The issue is specific to Next.js 16.0.x and occurs even when using the minimal global-error.tsx file or deleting the file entirely. The error is related to the internal error handling system and useContext null reference errors.

Recommendation

Apply workaround: Downgrade to Next.js 15.5.6 until the issue is resolved in Next.js 16, despite its own critical bug. This is because the issue is critical and blocks Next.js 16 adoption, and there is no available workaround that can be implemented from application code.

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