nextjs - 💡(How to fix) Fix useContext is null during SSG prerender in pnpm workspace (App Router, Next 14.2.x & 16 canary) [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#92839Fetched 2026-04-17 08:21:45
View on GitHub
Comments
1
Participants
2
Timeline
4
Reactions
0
Timeline (top)
closed ×1commented ×1labeled ×1locked ×1

Error Message

Generating static pages (0/39) ... TypeError: Cannot read properties of null (reading 'useContext') at t.useContext (.../next/dist/compiled/next-server/app-page.runtime.prod.js:12:109363) at d (.../apps/web/.next/server/chunks/846.js:66:22885) at p (.../apps/web/.next/server/chunks/846.js:66:14872) at au (.../next/dist/compiled/next-server/app-page.runtime.dev.js:35:10446) ... Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error

Root Cause

Reproducer reliably fails across 4 apps in the same workspace, on 2 Next versions, with pinned single-copy React. If someone on the Next team can confirm, it would be helpful to know whether pnpm shamefully-hoist=true or node-linker=hoisted resolves it — we have not yet tested those because they would change our install semantics project-wide.

Fix Action

Fix / Workaround

Every static route fails with the identical error. On Next 14, the compiled chunk offset 27451 decodes to Next's own usePathname() context lookup inside an error-boundary wrapper — the React dispatcher is null at that point.

Partial workaround (not a fix)

This suggests the RSC dispatcher is only missing on the SSG code path, not on the dynamic SSR path.

Code Example

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Home Single Language 10.0.26200
Binaries:
  Node: 20.x
  pnpm: 10.15.0
Relevant Packages:
  next: tested on 14.2.0, 14.2.35, and canary (16.2.1-canary.43) — all reproduce
  react: tested on 18.2.0 and 19.0.0 — both reproduce
  react-dom: matched to react version
  TypeScript: 5.4.x
Package manager: pnpm (workspace mode, 2-app minimal repro)

---

git clone https://github.com/saasmeld-holdco/next-ssg-usecontext-repro
cd next-ssg-usecontext-repro
pnpm install
pnpm --filter app-a build
# Crashes during "Generating static pages" with useContext null.

---

Generating static pages (0/39) ...
TypeError: Cannot read properties of null (reading 'useContext')
    at t.useContext (.../next/dist/compiled/next-server/app-page.runtime.prod.js:12:109363)
    at d (.../apps/web/.next/server/chunks/846.js:66:22885)
    at p (.../apps/web/.next/server/chunks/846.js:66:14872)
    at au (.../next/dist/compiled/next-server/app-page.runtime.dev.js:35:10446)
    ...
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error

---

TypeError: Cannot read properties of null (reading 'useContext')
    at <unknown> (.../app-a/.next/server/chunks/ssr/[root-of-the-server]__*.js:4:28956) {
  digest: '2041833152'
}
Export encountered an error on /_global-error/page: /_global-error, exiting the build.

---

PageNotFoundError: Cannot find module for page: /_document
RAW_BUFFERClick to expand / collapse

Title

useContext is null during SSG prerender in pnpm workspace (App Router, Next 14.2.x)

Body

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Tested on next@canary resolving to 16.2.1-canary.43 with React 19.0.0 — reproduces identically with the same useContext null error (.next/server/chunks/ssr/[root-of-the-server]__*.js). The bug is not a Next 14 regression; it spans at least Next 14.2.0, 14.2.35, and 16.2.1-canary.43.

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Home Single Language 10.0.26200
Binaries:
  Node: 20.x
  pnpm: 10.15.0
Relevant Packages:
  next: tested on 14.2.0, 14.2.35, and canary (16.2.1-canary.43) — all reproduce
  react: tested on 18.2.0 and 19.0.0 — both reproduce
  react-dom: matched to react version
  TypeScript: 5.4.x
Package manager: pnpm (workspace mode, 2-app minimal repro)

Which area(s) are affected?

App Router, Output (export/standalone), Runtime

Which stage(s) are affected?

next build (local), next build (Vercel)

Link to the code that reproduces this issue

https://github.com/saasmeld-holdco/next-ssg-usecontext-repro

git clone https://github.com/saasmeld-holdco/next-ssg-usecontext-repro
cd next-ssg-usecontext-repro
pnpm install
pnpm --filter app-a build
# Crashes during "Generating static pages" with useContext null.

Verified reproducing on [email protected] with [email protected] pinned via pnpm overrides (single React instance confirmed via readlink).

To Reproduce

  1. Create a pnpm workspace with 4 Next 14.2.x apps sharing a @saasmeld/ui package via workspace:*.
  2. Each app's root layout wraps children in NextIntlClientProvider plus a small PostHogProvider that uses usePathname().
  3. Run pnpm --filter @saasmeld/web build.

Note: a fully stripped layout (no providers, no hooks, no intl) and a trivial <h1> homepage still reproduce the crash. The issue is not provider-specific.

Expected Behavior

next build completes SSG for all static routes and emits .next/ artifacts.

Actual Behavior

On Next 14.2.x:

Generating static pages (0/39) ...
TypeError: Cannot read properties of null (reading 'useContext')
    at t.useContext (.../next/dist/compiled/next-server/app-page.runtime.prod.js:12:109363)
    at d (.../apps/web/.next/server/chunks/846.js:66:22885)
    at p (.../apps/web/.next/server/chunks/846.js:66:14872)
    at au (.../next/dist/compiled/next-server/app-page.runtime.dev.js:35:10446)
    ...
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error

On Next 16.2.1-canary.43 + React 19 (same error, different compiled chunk path):

TypeError: Cannot read properties of null (reading 'useContext')
    at <unknown> (.../app-a/.next/server/chunks/ssr/[root-of-the-server]__*.js:4:28956) {
  digest: '2041833152'
}
Export encountered an error on /_global-error/page: /_global-error, exiting the build.

Every static route fails with the identical error. On Next 14, the compiled chunk offset 27451 decodes to Next's own usePathname() context lookup inside an error-boundary wrapper — the React dispatcher is null at that point.

Things attempted that did NOT fix it

  • Pinning React + react-dom to 18.2.0 via pnpm overrides (confirmed single instance via readlink node_modules/react).
  • Bumping Next 14.2.0 → 14.2.35 via pnpm overrides.
  • Deleting apps/*/.next before every rebuild.
  • Disabling the next-intl plugin entirely.
  • Replacing all providers in the root layout with a bare <html><body><main>{children}</main></body></html>.
  • Replacing the homepage with export default () => <h1>x</h1>.

Partial workaround (not a fix)

Adding export const dynamic = "force-dynamic" to the root layout pushes past the useContext crash, then fails differently with:

PageNotFoundError: Cannot find module for page: /_document

This suggests the RSC dispatcher is only missing on the SSG code path, not on the dynamic SSR path.

Hypothesis

pnpm's symlinked workspace node_modules layout is interacting badly with Next 14's internal AsyncLocalStorage-based React dispatcher registration during SSG worker processes. The production runtime calls useContext before the dispatcher is populated.

Reproducer reliably fails across 4 apps in the same workspace, on 2 Next versions, with pinned single-copy React. If someone on the Next team can confirm, it would be helpful to know whether pnpm shamefully-hoist=true or node-linker=hoisted resolves it — we have not yet tested those because they would change our install semantics project-wide.

extent analysis

TL;DR

The most likely fix or workaround for the useContext null error during SSG prerender in a pnpm workspace with Next 14.2.x is to investigate the interaction between pnpm's symlinked workspace node_modules layout and Next's internal AsyncLocalStorage-based React dispatcher registration.

Guidance

  • Investigate the effect of using shamefully-hoist=true or node-linker=hoisted in pnpm to see if it resolves the issue, as this may change how modules are resolved and potentially fix the dispatcher registration issue.
  • Verify that the issue persists when using a different package manager, such as npm or yarn, to determine if the issue is specific to pnpm.
  • Consider testing with a minimal reproducible example outside of the pnpm workspace to isolate the issue and determine if it's related to the workspace setup.
  • Review the Next.js documentation and source code to understand how the AsyncLocalStorage-based React dispatcher registration works during SSG worker processes and how it might be affected by the pnpm workspace setup.

Example

No code snippet is provided as the issue is more related to the project setup and configuration rather than a specific code snippet.

Notes

The issue seems to be related to the interaction between pnpm's workspace setup and Next's internal dispatcher registration. The fact that adding export const dynamic = "force-dynamic" to the root layout pushes past the useContext crash but fails differently suggests that the issue is specific to the SSG code path.

Recommendation

Apply a workaround by investigating the use of shamefully-hoist=true or node-linker=hoisted in pnpm, as this may resolve the issue without requiring a full upgrade to a newer version of Next.js. This approach allows for a more targeted fix without introducing potential breaking changes.

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