nextjs - 💡(How to fix) Fix Build fails on Next.js 16.0.8 with TypeError: Cannot read properties of null (reading 'useContext') during /_global-error prerendering [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#86965Fetched 2026-04-08 02:08:11
View on GitHub
Comments
1
Participants
2
Timeline
9
Reactions
0
Timeline (top)
labeled ×5closed ×1commented ×1issue_type_added ×1

Error Message

// src/components/providers/motion-provider.tsx "use client"; import { LazyMotion, domAnimation } from "framer-motion"; export function MotionProvider({ children }: { children: React.ReactNode }) { return <LazyMotion features={domAnimation} strict>{children}</LazyMotion>; }

  1. Wrap children in root layout.tsx with MotionProvider
  2. Run npm run build
  3. Build fails with: Error occurred prerendering page "/_global-error"

Current vs. Expected behavior

Current behavior: Build fails during static generation with: TypeError: Cannot read properties of null (reading 'useContext') on the auto-generated /_global-error page.

Expected behavior: Build should succeed. This works correctly on Next.js 15.5.7 with the same configuration.

This is a regression from the fix applied in the now-locked Issue #85668.

Provide environment information

Root Cause

Root cause: The auto-generated /_global-error page is being prerendered in a way that calls React context hooks before the context provider is available. This affects any app using context-dependent libraries (Framer Motion, next-intl, etc.) in the root layout.

Fix Action

Fix / Workaround

Next.js 16.0.8: ❌ FAILS (/_global-error prerender error) Next.js 15.5.7: ✅ WORKS Workaround: Downgrade to Next.js 15.5.7

Code Example

// src/components/providers/motion-provider.tsx
   "use client";
   import { LazyMotion, domAnimation } from "framer-motion";
   export function MotionProvider({ children }: { children: React.ReactNode }) {
     return <LazyMotion features={domAnimation} strict>{children}</LazyMotion>;
   }

4. Wrap children in root layout.tsx with MotionProvider
5. Run npm run build
6. Build fails with: Error occurred prerendering page "/_global-error"

### Current vs. Expected behavior

Current behavior:
Build fails during static generation with:
TypeError: Cannot read properties of null (reading 'useContext')
on the auto-generated /_global-error page.

Expected behavior:
Build should succeed. This works correctly on Next.js 15.5.7 with the same configuration.

This is a regression from the fix applied in the now-locked Issue #85668.

### Provide environment information
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/vercel/next.js/issues/85668

To Reproduce

  1. Create a new Next.js 16 app with App Router

  2. Install Framer Motion: npm install framer-motion

  3. Create a MotionProvider that wraps LazyMotion in root layout:

    // src/components/providers/motion-provider.tsx
    "use client";
    import { LazyMotion, domAnimation } from "framer-motion";
    export function MotionProvider({ children }: { children: React.ReactNode }) {
      return <LazyMotion features={domAnimation} strict>{children}</LazyMotion>;
    }
  4. Wrap children in root layout.tsx with MotionProvider

  5. Run npm run build

  6. Build fails with: Error occurred prerendering page "/_global-error"

Current vs. Expected behavior

Current behavior: Build fails during static generation with: TypeError: Cannot read properties of null (reading 'useContext') on the auto-generated /_global-error page.

Expected behavior: Build should succeed. This works correctly on Next.js 15.5.7 with the same configuration.

This is a regression from the fix applied in the now-locked Issue #85668.

Provide environment information

Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 25.1.0
Binaries:
Node: 25.2.1
npm: 11.6.2
Relevant Packages:
next: 16.0.8
react: 19.2.1
react-dom: 19.2.1
typescript: 5.9.3
framer-motion: 12.23.25

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

Route Groups, Route Handlers, Dynamic Routes, Turbopack

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

next build (local)

Additional context

This appears to be a regression or continuation of Issue #85668, which was closed and locked.

Versions tested:

Next.js 16.0.8: ❌ FAILS (/_global-error prerender error) Next.js 15.5.7: ✅ WORKS Workaround: Downgrade to Next.js 15.5.7

Root cause: The auto-generated /_global-error page is being prerendered in a way that calls React context hooks before the context provider is available. This affects any app using context-dependent libraries (Framer Motion, next-intl, etc.) in the root layout.

The error occurs even with:

"use client" directive on provider components export const dynamic = "force-dynamic" on layouts Custom global-error.tsx that doesn't use context

extent analysis

TL;DR

Downgrade to Next.js 15.5.7 as a temporary workaround to resolve the build failure issue caused by the prerendering of the auto-generated /_global-error page.

Guidance

  • Verify that the issue is indeed a regression from the fix applied in Issue #85668 by checking the version history and comparing the behavior between Next.js 16.0.8 and 15.5.7.
  • Consider creating a custom global-error.tsx page that does not use React context to see if the issue persists, which can help isolate the problem.
  • If using other context-dependent libraries like next-intl, test if the issue is specific to Framer Motion or if it affects other libraries as well.
  • Keep an eye on the Next.js issue tracker for any updates or fixes related to this regression, as a future update may address this issue.

Example

No code example is provided as the issue is more related to version compatibility and regression rather than a specific code snippet that needs correction.

Notes

The provided workaround of downgrading to Next.js 15.5.7 may not be suitable for all projects, especially those that rely on features or fixes introduced in later versions. It's essential to weigh the benefits of using the latest version against the need to resolve this specific issue.

Recommendation

Apply the workaround by downgrading to Next.js 15.5.7, as it is the most straightforward way to resolve the build failure issue until a fix is available for the regression in Next.js 16.0.8.

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