Hi Blaine Garrett, Thanks for sharing a great writeup about a gnarly topic.
I have a minor correction to share.
You wrote:
"Note: This hierarchy optionally goes further with .env.development and .env.production (corresponding to the value of NODE_ENV)..."
But they don't actually correspond to the value of NODE_ENV. Next.JS bases its decision to read from .env.development vs .env.production on one thing only: whether you start the app with next dev or next start. That's all. NODE_ENV's value is irrelevant to that bit of Next-specific logic.
AFAICT you get the rest of the complicated picture right; hopefully my comment will inspire you to edit the post, or at least help ensure other readers aren't accidentally misled.
Again, I don't use this feature often so I hesitate to dig further at the moment. I tend to explicitly add the NODE_ENV to my command line args when running the prod build locally but it might be redundant in that next dev is implicitly NODE_ENV='development' and next start is NODE_ENV='production'. I'm tempted to see what NODE_ENV='development' next start produces. I'll have to try it out later. Anywho, thanks again for commenting and I'm glad these articles are still helping folks out after 2 years.
FTR I did exhaustive tests including NODE_ENV=development next start,
NODE_ENV=production next dev,
etc -- but I'm also currently stuck on v10.2.3. Maybe this has since changed in v11 or v12.
IMHO Next.js made a rare misstep in being both opinionated and rigid about proper support for true 12-factor config that lives in the runtime deploy environment. It probably stems from their original desire to stay "close" to create-react-app, and ofc whatever most closely aligns with Vercel's preferred hosting model. (shrug)
Chris Weekly
Hi Blaine Garrett,
Thanks for sharing a great writeup about a gnarly topic.
I have a minor correction to share. You wrote:
"Note: This hierarchy optionally goes further with
.env.developmentand.env.production(corresponding to the value of NODE_ENV)..."But they don't actually correspond to the value of NODE_ENV. Next.JS bases its decision to read from
.env.developmentvs.env.productionon one thing only: whether you start the app withnext devornext start. That's all. NODE_ENV's value is irrelevant to that bit of Next-specific logic.AFAICT you get the rest of the complicated picture right; hopefully my comment will inspire you to edit the post, or at least help ensure other readers aren't accidentally misled.
Slainte, Chris