Next.js Discord

Discord Forum

how to change .env file during build in app router

Unanswered
Northeast Congo Lion posted this in #help-forum
Open in Discord
Northeast Congo LionOP
i have three env fiel .env.qa .env.staging and .env.production i a deploying on ec2 and i want to use the .env.qa for a build and .env.staging for staging env how to do it

1 Reply

Burmese
I believe that the env loading behaviour of next is tightly coupled to the value of NODE_ENV So you could try setting NODE_ENV to qa and see if that env file gets loaded, but I'm not sure if it supports non-standard values.

In general I advocate for not relying on NODE_ENV and using your own flag (ex: APP_ENV) where you can set it up however you want, although this will not play nice with the built-in env loading behaviour or next.

I actually just released a new open-source tool that helps with env vars in Next.js that may help. You can set values based on each other in functions, and easily set up a switch based on another value to do whatever you want. You can also sync secrets with various backends and it does a bunch of other security related stuff.

Quick overview of what it can do here - https://dmno.dev/blog/dmno-nextjs-launch/

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