nextjs - ✅(Solved) Fix Next 16.2 won't load .env variables in Vercel at runtime [2 pull requests, 3 comments, 3 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#92116Fetched 2026-04-08 01:51:55
View on GitHub
Comments
3
Participants
3
Timeline
13
Reactions
0
Assignees
Timeline (top)
commented ×3assigned ×2cross-referenced ×2labeled ×2

Error Message

There's a hydration error printed to the browser console, as the static value is swapped out at runtime to undefined.

Fix Action

Fixed

PR fix notes

PR #92158: [test] Add suite for .env in App router based on existing Pages Router tests

Description (problem / solution / changelog)

We got reports that this behavior changed in 16.2. Since we don't have any tests for App router, I'm adding some.

Hopefully the existing tests catch the regression. Otherwise I'll just copy the case from https://github.com/vercel/next.js/issues/92116

Changed files

  • test/e2e/app-dir/app-env-config/.env (added, +19/-0)
  • test/e2e/app-dir/app-env-config/.env.development (added, +3/-0)
  • test/e2e/app-dir/app-env-config/.env.development.local (added, +2/-0)
  • test/e2e/app-dir/app-env-config/.env.local (added, +3/-0)
  • test/e2e/app-dir/app-env-config/.env.production (added, +3/-0)
  • test/e2e/app-dir/app-env-config/.env.production.local (added, +2/-0)
  • test/e2e/app-dir/app-env-config/.env.test (added, +3/-0)
  • test/e2e/app-dir/app-env-config/.env.test.local (added, +2/-0)
  • test/e2e/app-dir/app-env-config/app-env-config.test.ts (added, +204/-0)
  • test/e2e/app-dir/app-env-config/app/another-global/page.js (added, +3/-0)
  • test/e2e/app-dir/app-env-config/app/api/all/route.js (added, +44/-0)
  • test/e2e/app-dir/app-env-config/app/dynamic/page.js (added, +63/-0)
  • test/e2e/app-dir/app-env-config/app/env-display.js (added, +9/-0)
  • test/e2e/app-dir/app-env-config/app/global/page.js (added, +11/-0)
  • test/e2e/app-dir/app-env-config/app/layout.js (added, +7/-0)
  • test/e2e/app-dir/app-env-config/app/page.js (added, +64/-0)
  • test/e2e/app-dir/app-env-config/app/ppr/page.js (added, +46/-0)
  • test/e2e/app-dir/app-env-config/app/static/page.js (added, +49/-0)
  • test/e2e/app-dir/app-env-config/next.config.js (added, +24/-0)

PR #46: Handle .env files in node output path

Description (problem / solution / changelog)

Summary

  • detect project .env / .env.* files in the node output path and include them in each node function file map
  • when .env files are detected, resolve @next/env relative to next, include its loader file in the function files, and pass the loader path into the generated node handler
  • in the handler, load env files via the injected @next/env path only when enabled
  • emit a build-time warning during detection: "Detected .env file, it is strongly recommended to use Vercel's env handling instead"

x-ref: https://github.com/vercel/next.js/issues/92116

Changed files

  • .changeset/yellow-ears-camp.md (added, +5/-0)
  • packages/adapter/src/node-handler.ts (modified, +14/-2)
  • packages/adapter/src/outputs.ts (modified, +64/-0)

Code Example

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro
  Available memory (MB): 32189
  Available CPU cores: 14
Binaries:
  Node: 25.0.0
  npm: 11.6.2
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 16.2.1-canary.13 // Latest available version is detected (16.2.1-canary.13).
  eslint-config-next: N/A
  react: 19.2.4
  react-dom: 19.2.4
  typescript: 5.9.3
Next.js Config:
  output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/mordechaim/missing-env-repro

To Reproduce

Local version, working:

  1. Start the application locally (either dev or build/start)
  2. Observe both static, dynamic and cached contexts all have the correct value for MY_DOTENV

Deployed on Vercel:

  1. Open the Vercel deployed version at: https://missing-env-repro.vercel.app
  2. The static value is briefly shown the correct value (observable in the network tab page response) and quickly swapped out to undefined
  3. The dynamic value is undefined all along
  4. The cached value correctly shows the variable

Current vs. Expected behavior

Since updating to Next 16.2 the runtime doesn't have access to variables defined in a committed .env file. During build time they all resolve correctly.

Variables set via the Vercel environment variables UI are properly set.

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro
  Available memory (MB): 32189
  Available CPU cores: 14
Binaries:
  Node: 25.0.0
  npm: 11.6.2
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 16.2.1-canary.13 // Latest available version is detected (16.2.1-canary.13).
  eslint-config-next: N/A
  react: 19.2.4
  react-dom: 19.2.4
  typescript: 5.9.3
Next.js Config:
  output: N/A

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

Module Resolution, Runtime

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

Vercel (Deployed)

Additional context

I used cacheComponents to demonstrate the difference between build-time and dynamic access; but the same behavior is observed when using fully static (correctly set) and fully dynamic (missing) pages.

There's a hydration error printed to the browser console, as the static value is swapped out at runtime to undefined. The cached value is showing the value at build time, and since I added "use cache", the value isn't swapped out during hydration.

extent analysis

Fix Plan

To fix the issue of missing environment variables in Next.js

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