nextjs - 💡(How to fix) Fix Static build at multiple environments [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#89451Fetched 2026-04-08 02:02:52
View on GitHub
Comments
1
Participants
2
Timeline
6
Reactions
0
Timeline (top)
labeled ×2closed ×1commented ×1issue_type_added ×1

Error Message

Observe the build crash during the Generating static pages phase with a useContext error on /_global-error. The build fails during static generation of internal routes (/_global-error) with the following error: TypeError: Cannot read properties of null (reading 'useContext') Static generation should complete without context errors in internal error-boundary pages, especially when no custom logic is present in global-error.tsx. I am a fullstack developer working in an enterprise environment (Maersk). We need to generate builds targeted at a "Development" server environment (using Dev APIs) but with production optimizations. Next.js 16 seems to strictly lock next build to .env.production, ignoring external injection. The useContext error on _global-error prevents the build from finishing entirely. Error: Error occurred prerendering page "/_global-error". Read more: https://nextjs.org/docs/messages/prerender-error Export encountered an error on /_global-error/page: /_global-error, exiting the build. Error: Process completed with exit code 1.

Code Example

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11
Binaries:
  Node: 20.x.x
  npm: 10.x.x
Relevant Packages:
  next: 16.1.5
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.x.x
Next.js Config:
  output: standalone (or default)
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/Maersk-Global/anlonepage-app

To Reproduce

To Reproduce Create a Next.js 16.1.5 project using the App Router and Turbopack.

Define a .env.development file with various NEXT_PUBLIC_ variables.

Add a build:dev script to package.json: "env-cmd -f .env.development next build".

Run npm run build:dev on a Windows/PowerShell environment.

Observe that the build log explicitly states - Environments: .env.production.

Observe the build crash during the Generating static pages phase with a useContext error on /_global-error.

Current vs. Expected behavior

Current Behavior:

next build ignores the environment variables injected via env-cmd and forces the loading of .env.production.

The build fails during static generation of internal routes (/_global-error) with the following error: TypeError: Cannot read properties of null (reading 'useContext')

A flood of "unique key prop" warnings appears for top-level tags (<html>, <head>, <meta>) which are not explicitly handled in the user's code.

--

Expected Behavior:

next build should respect the environment variables provided by the shell or loading tools like env-cmd.

Static generation should complete without context errors in internal error-boundary pages, especially when no custom logic is present in global-error.tsx.

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11
Binaries:
  Node: 20.x.x
  npm: 10.x.x
Relevant Packages:
  next: 16.1.5
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.x.x
Next.js Config:
  output: standalone (or default)

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

Script (next/script)

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

next build (local)

Additional context

I am a fullstack developer working in an enterprise environment (Maersk). We need to generate builds targeted at a "Development" server environment (using Dev APIs) but with production optimizations. Next.js 16 seems to strictly lock next build to .env.production, ignoring external injection. The useContext error on _global-error prevents the build from finishing entirely.

Error: Check the top-level render call using <head>. See https://react.dev/link/warning-keys for more information. 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: '3200802800' } Export encountered an error on /_global-error/page: /_global-error, exiting the build. ⨯ Next.js build worker exited with code: 1 and signal: null Error: Process completed with exit code 1.

extent analysis

TL;DR

  • The issue can be resolved by ensuring that the env-cmd command correctly sets the environment variables for the next build process, potentially by modifying the build:dev script in package.json.

Guidance

  • Verify that the env-cmd command is correctly setting the environment variables by running a test command, such as env-cmd -f .env.development printenv, to check if the variables are being loaded as expected.
  • Modify the build:dev script in package.json to use the --env flag with next build, like "build:dev": "env-cmd -f .env.development next build --env" is not a viable solution since next build does not support an --env flag, instead consider using a cross-platform method to set environment variables.
  • Check the Next.js documentation for any specific recommendations on using environment variables with the App Router and Turbopack, as the issue might be related to the new features introduced in Next.js 16.
  • Consider setting the NODE_ENV environment variable to development before running the next build command to ensure that Next.js uses the correct environment configuration.

Example

No code example is provided as the issue is more related to the build process and environment variable configuration rather than a specific code snippet.

Notes

  • The issue seems to be specific to the combination of Next.js 16, App Router, Turbopack, and the use of env-cmd on a Windows/PowerShell environment.
  • The useContext error on /global-error might be a symptom of the underlying issue with environment variables not being correctly set.

Recommendation

  • Apply a workaround by modifying the build:dev script to correctly set the environment variables, potentially using a cross-platform method to ensure compatibility with different operating systems.

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