#handle custom .env with build & start command in 16.0.3 Page Router
1 messages · Page 1 of 1 (latest)
🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord
🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize
✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)
how about dotenv-cli?
"build:uat": "dotenv -e .env.uat -- next build"
"build:qa": "dotenv -e .env.qa -- next build"
I'm not sure about it, that's why I am asking!.. and then npm run start command will remain same?
well it depends
as I understand it, nextjs puts NEXT_PUBLIC_ env vars into the build, but anything without that prefix is only read at runtime
so you still have to provide those env vars on the server when the app runs
I will be deplyoing the App on Ubuntu Linux Server, so I'll be utilizing .env.* depending upon the Deployment, like .qa for QA Team, .uat for Business Team & .env.production for the Production Deployment
@tardy citrus
then you should we fine with these, just create start:uat, start:qa... scripts to provide runtime env
just for build command, not required for start command right?
x
i'm almost env variables without NEXT_PUBLIC_ prefix aren't inplaced in bundle
Sorry, but I didn't get you & understand you at all
"scripts": {
"dev:qa": "dotenv -e .env.qa -- next dev",
},
this is how my package.json scripts looks like, and below is the way I start the command:
npm run dev:qa
> induzkart_client@0.1.0 dev:qa
> dotenv -e .env.qa -- next dev
▲ Next.js 16.0.3 (Turbopack)
- Local: http://localhost:3000
- Network: http://192.168.1.3:3000
- Environments: .env.development
✓ Starting...
if you see, it's still taking the .env.development even though .env.qa exists
please help me @tardy citrus
the message you are seeing (- Environments: .env.development) comes from nextjs
if im right it lookups variables in following order: process.env > .env.{NODE_ENV}.local > .env.local > .env.{NODE_ENV} >.env or something similiar
so in your case:
- you load env variables from
.env.qausing dotenv clie - dotenv cli runs
next devwith.env.qacontent merged withprocess.env - nextjs loads
.env.development(.env.developmentsince it's in above order)
if nextjs docs about env variables are true, it will first pickup variables you already loaded from .env.qa before looking up in .env.development
if you want to get rid of - Environments: .env.development message - either remove, rename or move .env.development from project directory
Understood!.. but that's not right correct? when we say I need to load .env.qa then even whatever the precedence or order it is, Nextjs should consider taking .env.qa instead of what coming first in order, right
as per docs, it first lookups variable in process.env, and dotenv cli already merged .env.qa with process.env so you should be fine
to make sure you put TEST_VARIABLE=1 in .env.qa and TEST_VARIABLE=2 in .env.development
then in some RSC log process.env.TEST_VARIABLE
so how to make sure that it should take only the one like .env.qa only not the .env.development
assuming your app is under app/ directory, create app/env/ directory and put ur env files here
then include app/env/ in .gitignore
I am using Page Directory with Nextjs v16.0.3
any help on this to fix it?
it was just example, has nothing to do with app router/pages router
Ask to chat gtp.
If so you can get the correct anser.
Just I tried it.
I tried ChatGPT as well, even that wasn't working too
what the difference it makes?