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

NEXT_PUBLIC Environment Variables not avaliable on Client in Standalone #37537

Closed
1 task done
SIMULATAN opened this issue Jun 8, 2022 · 3 comments
Closed
1 task done
Labels
bug Issue was opened via the bug report template.

Comments

@SIMULATAN
Copy link

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #49~20.04.1-Ubuntu SMP Wed May 18 18:44:28 UTC 2022
Binaries:
  Node: 18.3.0
  npm: 8.11.0
  Yarn: N/A
  pnpm: N/A
Relevant packages:
  next: 12.1.7-canary.31
  react: 17.0.2
  react-dom: 17.0.2

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

Docker Image with Standalone

Describe the Bug

Environment Variables set at Runtime don't work on the client, only on the server, even when prefixed with NEXT_PUBLIC_.
This also happens with a loaded .env file

Expected Behavior

Environment Variables with the NEXT_PUBLIC_ prefix are useable in the browser

To Reproduce

  1. make a docker image with the standalone version
  2. run the image with a environment variable NEXT_PUBLIC_TESTVAR supplied via docker env vars
  3. try to access the variable in a page with process.env.NEXT_PUBLIC_TESTVAR - it'll return undefined
@SIMULATAN SIMULATAN added the bug Issue was opened via the bug report template. label Jun 8, 2022
@flex-jonghyen
Copy link

I have same problem on middleware

@balazsorban44
Copy link
Member

This is expected as per the docs:

The value will be inlined into JavaScript sent to the browser because of the NEXT_PUBLIC_ prefix. This inlining occurs at build time, so your various NEXT_PUBLIC_ envs need to be set when the project is built.

See:
https://nextjs.org/docs/basic-features/environment-variables#exposing-environment-variables-to-the-browser

@github-actions
Copy link
Contributor

github-actions bot commented Jul 9, 2022

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

3 participants