vercel / vercel/next.js

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

Open
#67,296 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Middleware Runtime Webpack
Dominant language
JavaScript
Stars
142k
Forks
32.5k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

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: https://github.com/vercel/next.js/discussions/39705#discussioncomment-4621291 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 https://github.com/vercel/next.js/discussions/39705

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked CodeSandbox reproduction and run the documented next dev steps, then deploy it to Vercel and compare the middleware logs for .env variables and Vercel-defined variables. Done means non-NEXT_PUBLIC_ variables behave consistently in Middleware in both environments, including variables referenced through process.env[name].

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nextjs
Domain
backend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.