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

runtime env are not working on production build #69375

Closed
1 task done
galgue opened this issue Aug 27, 2024 · 1 comment
Closed
1 task done

runtime env are not working on production build #69375

galgue opened this issue Aug 27, 2024 · 1 comment
Labels
examples Issue/PR related to examples

Comments

@galgue
Copy link

galgue commented Aug 27, 2024

Verify canary release

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

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.6.0: Fri Jul  5 17:56:41 PDT 2024; root:xnu-10063.141.1~2/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 8
Binaries:
  Node: 20.12.0
  npm: 10.5.0
  Yarn: 1.22.19
  pnpm: 9.7.0
Relevant Packages:
  next: 14.2.6 // Latest available version is detected (14.2.6).
  eslint-config-next: 14.2.6
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.5.4
Next.js Config:
  output: standalone

Which example does this report relate to?

none

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

cannot set env variables in run time, all env, not only the one that start with NEXT_PUBLIC_ are embedded in process.env in build time and cannot be changed.

Expected Behavior

be able to pass server env variables in run time.

To Reproduce

https://github.com/galgue/nextjs-runtime-env/tree/main
run npm run build
go to .next/standalone folder and run RUN_TIME=1 node server.js
in the browser browse into http://localhost:3000/api/health to get the env variable.

expect to see RUN_TIME=1 but we get undefined.

@galgue galgue added the examples Issue/PR related to examples label Aug 27, 2024
@vercel vercel deleted a comment Aug 27, 2024
@ztanner
Copy link
Member

ztanner commented Aug 27, 2024

Hi there -- thanks for the report. This is happening because Next.js is pre-generating your /api/health route at build time because there wasn't any indication that you needed to access dynamic data.

We changed this in Next 15 / canary, so route handlers are no longer cached by default. However in versions prior to 15, you'd need to add this to the top of your route handler to ensure it's always executed at runtime:

export const dynamic = "force-dynamic"

You should then see the RUN_TIME environment variable in your API endpoint.

@ztanner ztanner closed this as not planned Won't fix, can't repro, duplicate, stale Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
examples Issue/PR related to examples
Projects
None yet
Development

No branches or pull requests

2 participants