nextjs - ✅(Solved) Fix Build fails with "Cannot read properties of null (reading 'useState'/'useContext')" during static generation in Next.js 16.0.1 [1 pull requests, 9 comments, 5 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#85668Fetched 2026-04-08 02:14:35
View on GitHub
Comments
9
Participants
5
Timeline
20
Reactions
3
Assignees
Timeline (top)
commented ×9cross-referenced ×2referenced ×2subscribed ×2

Error Message

✓ Compiled successfully in 9.4s Generating static pages (0/10) ... Error occurred prerendering page "/test". Read more: https://nextjs.org/docs/messages/prerender-error TypeError: Cannot read properties of null (reading 'useState') at B (.next/server/chunks/ssr/8badd5da..js:2:9695) { digest: '2726912500' } Export encountered an error on /test/page: /test, exiting the build. ⨯ Next.js build worker exited with code: 1 and signal: null

Fix Action

Fix / Workaround

  1. Create a new Next.js 16.0.1 app with React 19
  2. Create ANY client component page that uses React hooks
  3. Add export const dynamic = 'force-dynamic' (workaround attempt)
  4. Run npm run build
  5. Build fails during static page generation

Workarounds Attempted:

WorkaroundResult
export const dynamic = 'force-dynamic'❌ Failed
output: 'standalone'❌ Failed
experimental.dynamicIO: true❌ Failed
Remove all hooks from component❌ Still fails
Use dynamic() with ssr: false❌ Turbopack error
Delete global-error.tsx❌ Auto-generated, still fails
Disable TypeScript❌ Failed
Clean .next directory❌ Failed

PR fix notes

PR #18: Fix documented errors from commit

Description (problem / solution / changelog)

Implemented fixes for the two documented errors:

  1. Fixed TypeScript error in lib/data.ts:

    • Changed table references from 'documentos' to 'jugador_documentos'
    • Aligns with actual database schema (migration 20250122000003)
  2. Fixed React Context prerendering error:

    • Downgraded React and React-DOM from 19.2.0 to 19.1.0
    • Resolves known bug in Next.js 16.0.x + React 19.2.0 (vercel/next.js#85668)
    • Clean reinstall of dependencies with verified single React version

Additional fixes for Next.js 16 strict prerendering:

  1. Footer component:

    • Converted to async Cache Component with 'use cache'
    • Allows new Date() usage during prerendering
  2. Dashboard layout:

    • Added Suspense boundary around Sidebar component
    • Handles client-side usePathname() hook properly
  3. Partido form:

    • Moved new Date() initialization to useEffect
    • Prevents prerendering errors in client components
  4. Disabled Cache Components feature:

    • Removed 'use cache' directives from lib/data.ts
    • Incompatible with Supabase cookies authentication
    • Commented out cacheComponents in next.config.ts

Changes follow documented solutions from:

  • docs/troubleshooting/build-error-solutions-2025.md (Strategy 1 & 2)
  • Community best practices for Next.js 16 + Supabase

Dependencies updated:

  • react: 19.2.0 → 19.1.0
  • react-dom: 19.2.0 → 19.1.0

Changed files

  • app/(dashboard)/layout.tsx (modified, +4/-1)
  • bun.lock (modified, +6/-6)
  • components/layout/footer.tsx (modified, +3/-1)
  • components/partidos/partido-form.tsx (modified, +9/-2)
  • lib/data.ts (modified, +6/-44)
  • next.config.ts (modified, +2/-1)
  • package.json (modified, +2/-2)

Code Example

'use client'

export const dynamic = 'force-dynamic'

import { useState } from 'react'

export default function TestPage() {
  const [count, setCount] = useState(0)

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  )
}

---

Compiled successfully in 9.4s
   Generating static pages (0/10) ...
Error occurred prerendering page "/test". Read more: https://nextjs.org/docs/messages/prerender-error
TypeError: Cannot read properties of null (reading 'useState')
    at B (.next/server/chunks/ssr/_8badd5da._.js:2:9695) {
  digest: '2726912500'
}
Export encountered an error on /test/page: /test, exiting the build.
  Next.js build worker exited with code: 1 and signal: null

---

## Verify canary release

Tested on:
-`[email protected]` (stable) - **FAILS**-`[email protected]` (latest canary as of 2025-11-01) - **FAILS**
**Both stable and canary have this bug.**


Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC
Binaries:
  Node: 22.20.0
  npm: 11.6.2
  pnpm: 10.19.0
Relevant Packages:
  next: 16.0.1
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: standalone

---

// app/imprint/page.tsx - Pure Server Component
import Link from 'next/link'

export default function ImprintPage() {  // Not even 'use client'!
  return (
    <div className="min-h-screen bg-spa-bg">
      <h1>Imprint</h1>
      <Link href="/">Back</Link>
    </div>
  )
}

---

Error occurred prerendering page "/imprint"
TypeError: Cannot read properties of null (reading 'useState')

---

const nextConfig = {
  output: 'standalone',
  reactStrictMode: true,
  experimental: {
    dynamicIO: true,  // Attempted fix - didn't work
  },
  typescript: {
    ignoreBuildErrors: true,
  },
}

---

{
  "dependencies": {
    "next": "16.0.1",
    "react": "19.2.0",
    "react-dom": "19.2.0",
    "@tanstack/react-query": "^6.0.0"
  }
}

---

Generating static pages (0/53) ...
Error occurred prerendering page "/forgot-password"
TypeError: Cannot read properties of null (reading 'useState')
    at B (.next/server/chunks/ssr/_8badd5da._.js:2:9695) {
  digest: '2726912500'
}

---

> tsc --noEmit
No errors found

---

> next dev
Ready on http://localhost:3002
All pages load correctly
No runtime errors
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/blackdiamond6390/nextjs-16-build-bug

To Reproduce

To Reproduce

Steps:

  1. Create a new Next.js 16.0.1 app with React 19
  2. Create ANY client component page that uses React hooks
  3. Add export const dynamic = 'force-dynamic' (workaround attempt)
  4. Run npm run build
  5. Build fails during static page generation

Minimal Example

app/test/page.tsx:

'use client'

export const dynamic = 'force-dynamic'

import { useState } from 'react'

export default function TestPage() {
  const [count, setCount] = useState(0)

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  )
}

Build Output:

✓ Compiled successfully in 9.4s
   Generating static pages (0/10) ...
Error occurred prerendering page "/test". Read more: https://nextjs.org/docs/messages/prerender-error
TypeError: Cannot read properties of null (reading 'useState')
    at B (.next/server/chunks/ssr/_8badd5da._.js:2:9695) {
  digest: '2726912500'
}
Export encountered an error on /test/page: /test, exiting the build.
 ⨯ Next.js build worker exited with code: 1 and signal: null

Current vs. Expected behavior

Current Behavior:

  • Build FAILS with TypeError: Cannot read properties of null (reading 'useState')
  • Affects ALL client component pages with React hooks
  • Even with export const dynamic = 'force-dynamic'
  • Even with output: 'standalone' in next.config.js
  • Even with experimental.dynamicIO: true

Expected Behavior:

  • Build should SUCCEED
  • Client components should be skipped during static generation OR rendered dynamically
  • export const dynamic = 'force-dynamic' should prevent prerendering
  • Similar pages work in Next.js 15.x

Provide environment information

## Verify canary release

Tested on:
- ✅ `[email protected]` (stable) - **FAILS** ❌
- ✅ `[email protected]` (latest canary as of 2025-11-01) - **FAILS** ❌

**Both stable and canary have this bug.**


Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC
Binaries:
  Node: 22.20.0
  npm: 11.6.2
  pnpm: 10.19.0
Relevant Packages:
  next: 16.0.1
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: standalone

Which area(s) are affected? (Select all that apply)

Not sure

Which stage(s) are affected? (Select all that apply)

next build (local)

Additional context

What I've tried (ALL FAILED):

  1. ✅ Added export const dynamic = 'force-dynamic' to pages → NO EFFECT
  2. ✅ Set output: 'standalone' in next.config.js → NO EFFECT
  3. ✅ Added experimental.dynamicIO: true → NO EFFECT
  4. ✅ Removed all React hooks from component → STILL FAILS
  5. ✅ Created minimal global-error.tsx without ANY hooks → STILL FAILS
  6. ✅ Added dynamic export to error.tsx → NO EFFECT

Observations:

  • npm run dev works PERFECTLY (no errors)
  • npm run build ALWAYS fails
  • 🔄 Error randomly affects different pages on each build attempt
  • 📝 Sometimes /forgot-password, sometimes /contact, sometimes /imprint
  • 🎯 Even pure server components without hooks fail with "Cannot read properties of null"

Affected Components:

Client components WITH hooks (expected to fail if SSG):

  • /app/contact/page.tsx - uses useSubscription() from React Query
  • /app/login/page.tsx - uses useState()
  • /app/register/page.tsx - uses useState()
  • /app/forgot-password/page.tsx - uses useState()

Special Next.js files (unexpected failures):

  • /app/error.tsx - uses NO hooks, still fails
  • /app/global-error.tsx - MINIMAL component, NO hooks, still fails
  • /_global-error (auto-generated) - fails with "Cannot read properties of null (reading 'useContext')"

Server components WITHOUT hooks (should NEVER fail):

  • /app/imprint/page.tsx - Pure server component, NO client code, NO hooks → FAILS!

Code Example - Imprint Page (Server Component, NO HOOKS, STILL FAILS):

// app/imprint/page.tsx - Pure Server Component
import Link from 'next/link'

export default function ImprintPage() {  // Not even 'use client'!
  return (
    <div className="min-h-screen bg-spa-bg">
      <h1>Imprint</h1>
      <Link href="/">Back</Link>
    </div>
  )
}

Build Error:

Error occurred prerendering page "/imprint"
TypeError: Cannot read properties of null (reading 'useState')

This suggests the error is NOT in our code, but in Next.js's build process itself.

Configuration Files:

next.config.js:

const nextConfig = {
  output: 'standalone',
  reactStrictMode: true,
  experimental: {
    dynamicIO: true,  // Attempted fix - didn't work
  },
  typescript: {
    ignoreBuildErrors: true,
  },
}

package.json dependencies:

{
  "dependencies": {
    "next": "16.0.1",
    "react": "19.2.0",
    "react-dom": "19.2.0",
    "@tanstack/react-query": "^6.0.0"
  }
}

Workarounds Attempted:

WorkaroundResult
export const dynamic = 'force-dynamic'❌ Failed
output: 'standalone'❌ Failed
experimental.dynamicIO: true❌ Failed
Remove all hooks from component❌ Still fails
Use dynamic() with ssr: false❌ Turbopack error
Delete global-error.tsx❌ Auto-generated, still fails
Disable TypeScript❌ Failed
Clean .next directory❌ Failed

Impact:

  • 🚫 Cannot build production bundle
  • 🚫 Cannot deploy to production
  • ✅ Development mode works fine
  • 📦 Affects ~15 pages in our application
  • 🔄 Error is non-deterministic (different pages fail on different builds)

Questions:

  1. Is this a known regression in Next.js 16.0.1?
  2. Is there a fix in canary?
  3. Should export const dynamic = 'force-dynamic' prevent prerendering completely?
  4. Why do server components without ANY client code fail with "Cannot read properties of null (reading 'useState')"?

Screenshots

Build log showing the error:

   Generating static pages (0/53) ...
Error occurred prerendering page "/forgot-password"
TypeError: Cannot read properties of null (reading 'useState')
    at B (.next/server/chunks/ssr/_8badd5da._.js:2:9695) {
  digest: '2726912500'
}

TypeScript check (passes fine):

> tsc --noEmit
✓ No errors found

Dev server (works perfectly):

> next dev
✓ Ready on http://localhost:3002
✓ All pages load correctly
✓ No runtime errors

Reproduction Repository

I can create a minimal reproduction repository if needed. Please confirm if you need it.

The issue is 100% reproducible in our production application with the exact steps above.


Priority: HIGH - This completely blocks production deployments for Next.js 16 applications using Client Components with React hooks.

extent analysis

TL;DR

The most likely fix for the build failure issue in Next.js 16.0.1 is to wait for an official patch or try using a canary version that may include a fix, as the issue seems to be related to a regression in the framework.

Guidance

  • Verify if the issue is a known regression in Next.js 16.0.1 by checking the official Next.js GitHub repository for similar issues or open a new issue to report the problem.
  • Attempt to use a canary version of Next.js to see if the issue is resolved in a newer version, as the issue affects both stable and canary versions tested.
  • Consider creating a minimal reproduction repository to help the Next.js team identify and fix the issue, as the problem is 100% reproducible in the production application.
  • Review the Next.js documentation and GitHub issues to see if there are any workarounds or temporary fixes available for the issue.

Example

No specific code example can be provided without more information about the root cause of the issue, but the provided code snippets in the issue body can be used as a starting point for further investigation.

Notes

The issue seems to be related to a regression in Next.js 16.0.1, and the provided workarounds have not been successful in resolving the issue. The problem affects both client components with React hooks and server components without hooks, making it a complex issue to debug.

Recommendation

Apply a workaround by waiting for an official patch or trying a canary version, as the issue seems to be related to a regression in the framework. The reason is that the issue affects both stable and canary versions tested, and the provided workarounds have not been successful in resolving the issue.

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