Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variables defined in .env files that do not begin with NEXT_PUBLIC_ are not available in Middleware deployed to Vercel #67296

Open
ijxy opened this issue Jun 29, 2024 · 2 comments
Labels
bug Issue was opened via the bug report template. Developer Experience Issues related to Next.js logs, Error overlay, etc. Middleware Related to Next.js Middleware Runtime Related to Node.js or Edge Runtime with Next.js. Webpack Related to Webpack with Next.js.

Comments

@ijxy
Copy link

ijxy commented Jun 29, 2024

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/flamboyant-cray-pwlc82

To Reproduce

  1. next dev
  2. observe that the middleware logs value as defined in .env
  3. Deploy to Vercel
  4. observe that the middleware logs undefined instead of the value defined in .env

Current vs. Expected behavior

Current

Variables defined in .env files that do not begin with NEXT_PUBLIC_ are not available in Middleware deployed to Vercel.

BUT, all variables defined in Vercel are available in Middleware. This inconsistency is very confusing.

.env files offer more functionality, e.g. MY_PATH="https://${VERCEL_URL}/some/path", so it would be great if it just worked.

I have tried this workaround: #39705 (comment) but it extremely limited since some dependencies access variables using other syntax like process.env[name].

Expected

Consistent behaviour. If all environment variables defined in Vercel are made available to middleware, so too should all environment variables defined in .env files.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 20.6.0: Tue Feb 22 21:10:42 PST 2022; root:xnu-7195.141.26~1/RELEASE_ARM64_T8101
  Available memory (MB): 8192
  Available CPU cores: 8
Binaries:
  Node: 20.10.0
  npm: 10.2.3
  Yarn: N/A
  pnpm: 8.15.4
Relevant Packages:
  next: 14.2.4 // Latest available version is detected (14.2.4).
  eslint-config-next: 14.2.4
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.5.2
Next.js Config:
  output: N/A

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

Developer Experience, Middleware, Runtime, Webpack

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

Vercel (Deployed)

Additional context

This issue is related to Vercel deployment, see the discussion here #39705

@ijxy ijxy added the bug Issue was opened via the bug report template. label Jun 29, 2024
@github-actions github-actions bot added Developer Experience Issues related to Next.js logs, Error overlay, etc. Middleware Related to Next.js Middleware Runtime Related to Node.js or Edge Runtime with Next.js. Webpack Related to Webpack with Next.js. labels Jun 29, 2024
@priyxansh
Copy link

Generally, when you load environment variables from .env* files, all the Non-NEXT-PUBLIC variables are only available in the Node.js runtime and not in the Edge runtime. This remains true when deployed on vercel too.
However when you configure variables through Vercel's project settings, all environment variables are made available to the edge runtime regardless of their prefix. This explains the inconsistency that you have been experiencing.

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 NEXT_PUBLIC_. We strongly recommend managing your environment variables in Project Settings
instead, from where all environment variables are available.

You can refer to these links to the NextJS documentation:

@ijxy
Copy link
Author

ijxy commented Jul 1, 2024

Ok, but why?

It doesn't make sense as it is now. You can't make the case that it is about security, because then it would also apply to non-NEXT_PUBLIC variables defined inside Vercel as well.

It is an inconsistency that makes it impossible to set new environment variables that depend on the VERCEL_ system environment variables, such as MY_ENV="https://${VERCEL_URL}"

It isn't always possible to do this in code--some libs only read environment variables (which is also a poor choice, but not the point here).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Developer Experience Issues related to Next.js logs, Error overlay, etc. Middleware Related to Next.js Middleware Runtime Related to Node.js or Edge Runtime with Next.js. Webpack Related to Webpack with Next.js.
Projects
None yet
Development

No branches or pull requests

2 participants