nextjs - 💡(How to fix) Fix @next/env loads .env.production.local when NODE_ENV is set to development [4 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#84316Fetched 2026-04-08 02:19:52
View on GitHub
Comments
4
Participants
2
Timeline
9
Reactions
0
Timeline (top)
commented ×4closed ×1issue_type_added ×1labeled ×1

Code Example

echo 'TEST_VAR=local' > .env.local
echo 'TEST_VAR=development' > .env.development.local
echo 'TEST_VAR=production' > .env.production.local

---

NODE_ENV=development ./next-env --debug node --eval "console.log('NODE_ENV:', process.env.NODE_ENV, 'TEST_VAR:', process.env.TEST_VAR)"

---

$ npx next info
/bin/sh: yarn: command not found
/bin/sh: pnpm: command not found

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.6.0: Mon Jul 14 11:29:54 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T8122
  Available memory (MB): 24576
  Available CPU cores: 8
Binaries:
  Node: 22.14.0
  npm: 10.9.2
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 15.5.4 // Latest available version is detected (15.5.4).
  eslint-config-next: N/A
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.9.2
Next.js Config:
  output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/ravinggenius/next-env-test

To Reproduce

  1. Clone the repo; install dependencies
  2. Create minimal environment files:
echo 'TEST_VAR=local' > .env.local
echo 'TEST_VAR=development' > .env.development.local
echo 'TEST_VAR=production' > .env.production.local
  1. Run the following command:
NODE_ENV=development ./next-env --debug node --eval "console.log('NODE_ENV:', process.env.NODE_ENV, 'TEST_VAR:', process.env.TEST_VAR)"

Current vs. Expected behavior

./next-env is a script I'm working on to execute a sub-command in an environment with variables loaded according to the Next env load order.

Current behavior

@next/env appears to load production env files, even when NODE_ENV is set to "development".

Expected behavior

@next/env should follow the documented env file loading rules. When NODE_ENV is set to "development", .env.production.local should not be loaded.

Provide environment information

$ npx next info
/bin/sh: yarn: command not found
/bin/sh: pnpm: command not found

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.6.0: Mon Jul 14 11:29:54 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T8122
  Available memory (MB): 24576
  Available CPU cores: 8
Binaries:
  Node: 22.14.0
  npm: 10.9.2
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 15.5.4 // Latest available version is detected (15.5.4).
  eslint-config-next: N/A
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.9.2
Next.js Config:
  output: N/A

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

Not sure

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

Other (Deployed)

Additional context

No response

extent analysis

TL;DR

The issue can be resolved by ensuring that the @next/env script correctly loads environment variables based on the NODE_ENV setting, potentially by adjusting the script to follow the Next.js environment variable load order.

Guidance

  • Verify that the @next/env script is correctly implemented to load environment variables according to the Next.js environment variable load order, which prioritizes files based on the NODE_ENV setting.
  • Check the next-env script for any hardcoded references to production environment files that might be causing the issue.
  • Test the script with different NODE_ENV settings to ensure it behaves as expected in various environments.
  • Review the Next.js documentation on environment variables to ensure the script aligns with the recommended practices.

Example

No specific code example can be provided without modifying the existing next-env script, but ensuring the script checks the NODE_ENV and loads the corresponding environment file (e.g., .env.development.local for NODE_ENV=development) is crucial.

Notes

The provided information suggests an issue with how the @next/env script handles environment variables, but without the script's code, it's challenging to provide a precise fix. The solution likely involves adjusting the script to correctly follow the Next.js environment variable load order.

Recommendation

Apply a workaround by manually ensuring the correct environment files are loaded based on the NODE_ENV setting, as directly fixing the @next/env script requires its source code.

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