Next.js Discord

Discord Forum

Does NextJS load .env or not?

Unanswered
Golden northern bumble bee posted this in #help-forum
Open in Discord
Golden northern bumble beeOP
I build and run a server. Then I change some variables in the .env file but my changes aren't applied. Why? Should I clear some cache?

17 Replies

@Golden northern bumble bee I build and run a server. Then I change some variables in the `.env` file but my changes aren't applied. Why? Should I clear some cache?
Normally if you change something in your env. variables, your server will automatically reload it. Also your paths and everything will be recompiled: (see attached)
Golden northern bumble beeOP
Yeah, but those are for server-side variables, right?
NEXT_PUBLIC ones are built-in
@Golden northern bumble bee Yeah, but those are for server-side variables, right?
I just tried it for both:
SOME_SECRET_ENV=hello_world
NEXT_PUBLIC_ENV=hello_other_world

And both times it got reloaded and recompiled: (see attached)
Golden northern bumble beeOP
Thanks, let me try it as well. Our main project is still using an old version of NextJS, I'm gonna check the latest version instead
yea, try updating to the latest stable version. Are you using app router as well?
Golden northern bumble beeOP
No 🙂
Let me clarify. I mean npm run build and npm run start. Not npm run dev
It doesn't seem to be reloading in my case on the latest NextJS
Ahhh then it makes sense, yea. You need to either redeploy your site (at vercel for example) or rebuild and restart the server
@B33fb0n3 Ahhh then it makes sense, yea. You need to either redeploy your site (at vercel for example) or rebuild and restart the server
Golden northern bumble beeOP
Yeah, and I want to fix this. Because it doesn't look like a correct behavior. When we build a docker image of for a deployment, it shouldn't include env variables like paths to services. Otherwise we would need to rebuild the image every time we decide to deploy to a specific instance
@Golden northern bumble bee Yeah, and I want to fix this. Because it doesn't look like a correct behavior. When we build a docker image of for a deployment, it shouldn't include env variables like paths to services. Otherwise we would need to rebuild the image every time we decide to deploy to a specific instance
it is the correct behavior, public env vars are replaced during build time in the client bundle because there is no "environment" in a client build. if you want new values you need to rebuild the project
@Rafael Almeida it is the correct behavior, public env vars are replaced during build time in the client bundle because there is no "environment" in a client build. if you want new values you need to rebuild the project
Golden northern bumble beeOP
We don't use the api-part of NextJS, so we use .env file to store URLs for backend services that we access. We need to switch them and we shouldn't built them into the image.
But I think I figured out how to achieve this already thanks to a neighbouring thread.
you would need to either fetch these values from an API (or SSR the page) or do your own search-and-replace during the image startup in the build output
Burmese
I just released a new open-source tool that helps with env vars in Next.js that will help wtih this exact issue. It lets you control which env vars will be bundled at build time vs kept dynamic, and decouples that concept from which vars are "public".

It will also trigger a server reload when you change anything, and there are a ton of other cool security related features as well.

https://dmno.dev/blog/dmno-nextjs-launch/

Would love to hear what you think! Also happy to help you get set up 🙂