vercel / next.js

The React Framework
https://nextjs.org
MIT License
126.99k stars 26.99k forks source link

Inconsistently managing .env files between edge and nodejs runtimes #65166

Closed freaz closed 6 months ago

freaz commented 6 months ago

Link to the code that reproduces this issue

https://github.com/freaz/nextjs_edge_env_files

To Reproduce

use .env, .env.production, ... to set default values (.e.g: AUTH0_BASE_URL="https://$VERCEL_BRANCH_URL"), than use the value with page or route using edge runtime.

Current vs. Expected behavior

On nodejs runtime env variable is properly set, but on edge it is missing. Expected is to load env vars from .env, .env.production, ... files the same way as for nodejs runtime.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.2.0: Wed Nov 15 21:55:06 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6020
  Available memory (MB): 32768
  Available CPU cores: 12
Binaries:
  Node: 22.0.0
  npm: 10.5.1
  Yarn: 1.22.19
  pnpm: 8.6.3
Relevant Packages:
  next: 14.2.3 // Latest available version is detected (14.2.3).
  eslint-config-next: N/A
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.4.5
Next.js Config:
  output: N/A

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

Runtime

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

Vercel (Deployed)

Additional context

No response

philwolstenholme commented 6 months ago

Hi,

I think this is expected behaviour on Edge:

Good to know: When deploying your Next.js application to Vercel, your environment variables in .env* files will not be made available to Edge Runtime, unless their name are prefixed with NEXTPUBLIC. We strongly recommend managing your environment variables in Project Settings instead, from where all environment variables are available.

https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables

https://github.com/vercel/next.js/discussions/39705 has some more information too.

I worked around the problem by running a script as part of our CI process that takes environment variables from the .env files and uses the Vercel API to add them as environment variables to the Vercel project settings, being careful not to override any variables that we don't want to be accidentally overridden.

freaz commented 6 months ago

Hey @philwolstenholme, thanks for the reply. You are right. Unfortunately the main use case for .env.production is to set ENV variables based on values set by Vercel. For example VERCEL_BRANCH_URL to setup Auth0 (setting AUTH0_BASE_URL for previews).

With the current state, it requires either manual deployment step setting AUTH0_BASE_URL or using non-default variable names in the code.

Also, it is quite confusing (and bad DX) to have ENV variables configured in the dashboard available in runtime and from .env* files only with a public prefix.

I understand now how it works for end users, and can imagine what is going on when code is deployed to the edge. So this isn't a bug per se, but I still believe there is space for improvement.

github-actions[bot] commented 6 months ago

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.