Replies: 22 comments 46 replies
-
Beta Was this translation helpful? Give feedback.
-
I'd like to offer one more additional solution: Loading from a configuration file. Working Demo |
Beta Was this translation helpful? Give feedback.
-
Would NextJS still be managing This RFC would help solve the current limitation of managing ENV vars for custom application environments but still doesn't take into account some other (seemingly large) limitations: Robust configuration management solution Tied to the build execution |
Beta Was this translation helpful? Give feedback.
-
It would be nice to be able to fetch the current environment from The problem with command-line argument is that it ends up being hard-written in your |
Beta Was this translation helpful? Give feedback.
-
I think solution 1 is the correct move. For instance, we run a production version of our app in sandbox mode (using a test database). I still expect I think we should keep I find the current |
Beta Was this translation helpful? Give feedback.
-
Thanks @flybayer for the detailed RFC and sharing. As correctly pointed out by @eric-burel, I think an env variable is a more universal solution. Prior to finding this RFC, I opened this PR which shows how this might be implemented (I chose |
Beta Was this translation helpful? Give feedback.
-
We have multiple beds for our product, not only dev and production. For example we have a test bed, and maybe we have multiple production environments in different datacenters, etc.. Each such bed has different values for the configs. The current environment variables implementation in next.js is too restricting. This RFC would probably solve the problem for us. |
Beta Was this translation helpful? Give feedback.
-
Thank you for opening this discussion! When Next.js introduced Can you provide examples where this solves setting |
Beta Was this translation helpful? Give feedback.
-
I am currently upgrading to latest version of everything, and I struggle to load variables in Cypress using Webpack 5. I think this is related to the fact that Webpack 5 no longer polyfills Node related stuffs, which seems to include allowing to use I am adding this: plugins: [
new webpack.DefinePlugin({
"process.env": JSON.stringify(process.env),
}),
], But haven't yet found a way to call No idea why the doc propose an async usage either in the documentation, as this function seems synchronous? It might be interesting to extend this RFC to "loading for different environments AND tools", because the usage is definitely related to the continuous integration pipeline. Edit: final code that seems to work (but not really documented) in my Cypres config: plugins: [
new webpack.DefinePlugin({
// TODO: also load variables from .env.test
// @see https://github.com/vercel/next.js/discussions/25764
"process.env": JSON.stringify(loadEnvConfig(process.cwd()).combinedEnv),
}),
], |
Beta Was this translation helpful? Give feedback.
-
👋 I'm using Vercel domains to host the same Nextjs app, and associate it with different back-end APIs using |
Beta Was this translation helpful? Give feedback.
-
I think this RFC is great and provides a flexible solution with minimal change. That being said, I think ALL front end frameworks need to stop baking in environment specific config into the build. Environment specific config should be resolved at runtime. Here's an interesting project that does exactly that and works with Next.js and CRA .env files. https://github.com/andrewmclagan/react-env |
Beta Was this translation helpful? Give feedback.
-
I think we can take que from Angular build here. By default build the code in production mode and just use environment files to load variables specific to environment (And still provide an option to run on IDE/Locally) . This is so much and easy to understand
|
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
@flybayer do you plan to submit a PR for this topic and @leerob would you see a chance to get the PR merged into the core? I'd really love to see this enhancement on the short update regarding the RFC specs: guess we should also make the path to the env files configurable. having them all on the project root is a bit cluttered imo |
Beta Was this translation helpful? Give feedback.
-
I faced this problem today. Suggestion for Don't use This is really bad solution so hoping to see PR for better enviroment managment. |
Beta Was this translation helpful? Give feedback.
-
I like the way how vite js using mode concept to load the env variables, have a look guys ~ https://vitejs.dev/guide/env-and-mode.html |
Beta Was this translation helpful? Give feedback.
-
thanks so much for this workaround --- saved the day |
Beta Was this translation helpful? Give feedback.
-
As mentioned by @EgorZotov the proposed workaround by @flybayer won't work if you have |
Beta Was this translation helpful? Give feedback.
-
Just going to add my two cents here. Having to use the production environment variables for staging is problematic for me because of:
|
Beta Was this translation helpful? Give feedback.
-
Solution 🔥With the recent Blitz 2.0 pivot, the blitz CLI is now a small standalone thing you can use to solve env variable loading with next.js.
This is a super thin wrapper over next cli with no perf overhead that:
|
Beta Was this translation helpful? Give feedback.
-
Rather than having multiple files for env by using |
Beta Was this translation helpful? Give feedback.
-
I am using package.json's script to copy different environment secrets to
I won't say its ideal but get the job done in my case. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
Apps have different runtime environments like
test
andstaging
that require different environment variables.The current Next.js env file loading as introduced in this RFC is too restricting and confusing.
NODE_ENV
.env.test
butNODE_ENV=test next build
fails, which is confusingNODE_ENV
, Next.js doesn't currently load files like.env.staging
(unlike dotenv-flow for example)This is a very common confusion and complaint: #12077, #17032, #19046, #12737, #12772, #9123, #22755, #12077, #14609, #14240, #18253, #10129
Proposed Solution
-e ENVIRONMENT_NAME
flag tonext dev
,next build
, andnext start
next dev -e staging
would load.env.staging
,.env
, etcAPP_ENV
as an official replacement ofNODE_ENV
for application environmentAPP_ENV=development
fornext dev
APP_ENV=production
fornext build
andnext start
APP_ENV=staging next start
@next/env
to support loading files for any environment like.env.staging
As you can see by browsing through the issues linked above, the
APP_ENV
pattern is one used by many of us and is also the official recommendation (source). But how to implement that is still left to the user with no good solution.Breaking Changes?
None based on official behavior which is that you can't use
NODE_ENV
. That said,NODE_ENV=test next start
was working in the past. However that no longer works, see #25491.To aid users who have been relying on .env files being loaded by
NODE_ENV
, we could optionally still useNODE_ENV
for loading files if it's present and neither-e
orAPP_ENV
is set.Downsides?
I see zero downside for adding the
-e
flag.For automatically loading .env files based on
APP_ENV
, it's possible someone is usingAPP_ENV
for something totally unrelated and this could produce some unexpected behavior. That said, I think it's safe to say most who are usingAPP_ENV
with Next.js are using it for exactly this purpose. For example, my current code setsAPP_ENV=production
inside.env.production
.Alternates
Only adopt
-e
orAPP_ENV
instead of both.Use
--env
instead of-e
Current Workarounds
Manually
Manually load the correct .env files via some hand rolled scripts
Via next.config.js
APP_ENV=staging next build && APP_ENV=staging next start
Beta Was this translation helpful? Give feedback.
All reactions