NEXT_PUBLIC environment variables not working in productoin
Answered
Masai Lion posted this in #help-forum
Masai LionOP
I am trying to set an environment variable using NEXT_PUBLIC_VAR=value and it works when dev mode is on but when I build and start next the environment variable stays undefined. I'm using
process.env.NEXT_PUBLIC_VAR to get the variable16 Replies
@Turkish Angora did you solve it ?
Masai LionOP
I figured out the problem was that next doesn't load env vars on build time from .env
Masai LionOP
You have to set them with dockerfile
Answer
@Masai Lion You have to set them with dockerfile
Cimarrón Uruguayo
or you can copy the env file in your dockerfile
@Cimarrón Uruguayo or you can copy the env file in your dockerfile
Masai LionOP
I'm doing that with docker compose
@Masai Lion I'm doing that with docker compose
Cimarrón Uruguayo
sure that also works
Masai LionOP
But I guess that docker compose loads env during runtime
So it doesn't work
Cimarrón Uruguayo
oh
ok
yeah to be safe
just copy the .env file
into ur container environment
so that nextjs will automatically pickup the file
when you run the build command
Masai LionOP
Yeah