Replies: 5 comments 4 replies
-
Hey @barankyle I believe I have a similar set up to you, my app sets Are you currently setting As you are using kubernetes I assume you have a docker image that builds a container to run your the next.js app. Do you mind sharing your Dockerfile? If I were you I would try building and running your container locally to see if the variables have been set correctly |
Beta Was this translation helpful? Give feedback.
-
Hello, I have sort of the same issue but I'm not using kubernetes or helm. This is my case: nextjs v9.4.4 with a custom server where I use dotenv as a devdepency, I have a .env file locally(it's in .gitignore). |
Beta Was this translation helpful? Give feedback.
-
The problem that @barankyle is experiencing is caused by Automatic Runtime Optimization as described here: https://nextjs.org/docs/api-reference/next.config.js/runtime-configuration. The environment variables page (unfortunately) makes no mention of this: https://nextjs.org/docs/basic-features/environment-variables We just spend a few hours debugging this ourselves. It might be good to add this mention to the environment variables page as well. So the difference lies between build-time environment variables and runtime environment variables. The reason that @malimccalla's configuration works, is indeed because he has ARGS/ENV in his docker file, thus passing build-time environment variables. Those are then baked in by the optimization, they're no longer read at runtime. |
Beta Was this translation helpful? Give feedback.
-
Could be also related to the fact that you need to add non-public variables you want to use at runtime that must be added to the build configuration. See https://github.com/UnlyEd/next-right-now/blob/v2-mst-aptd-at-lcz-sty/next.config.js#L19-L34 |
Beta Was this translation helpful? Give feedback.
-
Hope this helps: Also make sure you restart your server |
Beta Was this translation helpful? Give feedback.
-
I just upgraded to 9.4 to try to solve my client-side environment variable troubles, but that doesn't seem to have fixed it, either.
My deployment is on Kubernetes via Helm. I'm setting an ENV_VAR 'NEXT_PUBLIC_API_SERVER' as one of the values passed into Helm. It's definitely being set properly, because if I go into the pod and run
echo $NEXT_PUBLIC_API_SERVER
, I get the value I expect.I'm using it in the client code in one place, which looks like this:
export const apiUrl = process.env.NODE_ENV === 'production' ? process.env.NEXT_PUBLIC_API_SERVER : 'http://localhost:3030'
This works fine when I'm testing locally, albeit when i swap 'production' for 'development'. But in production, process.env is empty in the code sent to the browser.
I don't have a .env file anywhere in this codebase, and dotenv is not in the package.json and is not installed.
What am I missing?
Beta Was this translation helpful? Give feedback.
All reactions