-
Notifications
You must be signed in to change notification settings - Fork 12k
Managing large amounts of environment variables #28661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
With export VAR_1=“foo”
$ ng build --define="VAR_1='$VAR_1'" We recently discussed using |
The original request for issue #4318 was asking for a way to pass system environment variables from the command line as shown by this example from the original issue: For cases where there is a larger list of values, a JSON file that is imported into the application at needed locations is an option and a potential replacement for the old environment file concept. Not only can the JSON file be tree-shaken based on usage but there is also full IDE support and build type-checking to ensure values are used as expected within the application. Combined with the As an example, here is a package.json excerpt and usage in an application file:
|
Just to expand on some of the implicit requirements of this request, it seems like
Command line I agree One potential benefit to |
This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list. You can find more details about the feature request process in our documentation. |
Honestly @dgp1130 explained it much better then I did. Essentially If security is a concern (accidentally leaking environment variables that shouldnt be) then we can copy react/nextjs where only variables starting with If you look at the original ticket the conversation continued even after the PR was merged indicating it did not address the issue for a lot of users. I disagree with @clydin suggestion though since to me that is the same thing as the |
Can you expand on your actual use case? How many environment variables are typically used? Won't the values need to be generated somewhere in all cases as well? |
So the most recent use case is for an Ionic/Angular Hybrid Application (1 codebase = web app + android app + ios app).
Lets use the example of the same app version being deployed on 3 environments. Environment 2: Environment 3: Notice the API points to a different domain on each environment. The vendor JS needs to be included in the Since this is run on a mobile app even if we wanted to load a runtime config we would need to have hardcoded domain of where to load the configuration since domain based path routing wouldn't work, but this issue would exist on Angular universal running on the server as well my example is just mobile but there would be other use cases. The environment variables would be set on the build machine or CI/CD pipeline configuration. So our devops team can configure: They can run the |
Thank you for the additional information. We will be discussing this further with the team and attempt to provide an ergonomic solution to these type of use cases. As to prefixing, If dotenv files are a preferred way of storing key/value pairs then there is the potential for an additional define-based option (e.g., |
Haha i agree NODE_ENV was definately a mistake.
|
The team will still be discussing this issue. But from further reading of the above details, a short script that runs pre-build may be a strong option for that use case. Something similar to the following could be used:
And this could be added to the
The advantages here would be complete flexibility as to the prefix (or even an allow list instead), the ability to adjust values, add entries from multiple sources, add defaults, or fail early if required entries are missing. A development only JSON file could even be checked in with local development values that would be overwritten on CI. Since it would be an imported JSON file, IDE support and build time diagnostics would be possible and full details of all usages throughout the application would be available. Plus this setup allows for a transition to runtime configuration in the future, if preferred, by moving the generated file to an asset that is served instead of imported. |
@clydin yep this makes sense and its a very elegant solution. Its essentially what we are currently doing just less organized i would say. {
"scripts": {
"build": "dotenv() ng build"
} That way angular itself is not dependent on dotenv (which i think is smart) but applications can still leverage it very easily if they decide to use it. |
From your use case from above, I thought The downside of adding something like what i described into the build system itself is that it removes all of the flexibility and extensibility that I mentioned in the last paragraph. |
Good point I should clarify that if a project WANTS to use dotenv it would work. For my use case you are correct we would not have .env files in the pipelines but having them in local would work. |
Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends. Find more details about Angular's feature request process in our documentation. |
have encrypted you could use them with ng build like:
or alternatively use
|
For those who need an alternative solution, I have forked angular-builders recently to match these needs and also make use of esbuild and vite under the hood. I use it in two production apps and it only supports the latest Angular release. Examples for the use of esbuild Define are also given (can be extended with dotenv), as well as usage with GraphQL. For more compatibility I suggest using angular-builders builders. |
ngx-env/builder is what some use as a fallback solution. @Component({
selector: "app-footer",
template: `{{ branch }} - {{ commit }}`,
})
export class MainComponent {
branch = import.meta.env.NG_APP_BRANCH_NAME; // Recommended
commit = process.env.NG_APP_COMMIT; // Deprecated
} <!-- index.html -->
<head>
<title>NgApp on %NG_APP_BRANCH_NAME% - %NG_APP_COMMIT%</title>
</head> Define Environment variables In .env, .env.* files
ng serve # env files will be injected as a define property of the CLI builder. Or in command line (or in your CI config) to overload them NG_APP_ENABLE_SENTRY=true ng build Adding
We'd love to have a similar DX supported natively in Angular CLI though. |
A while back when I switched from Vue back to Angular after a few years not touching Angular development, my habit was looking for IMO, this missing |
Command
build, serve, test, e2e
Description
I am reopening #4318.
Essentially we need a way to pass environment variables to the build/serve commands. environments.ts file is not a viable solution as you would need to end up with hundreds of environment.ts files depending on all the permutations per environment.
The solution to #4318 also does not address this since you need to know the
define
values when running the commandng build <define> <define> <define>....
Which means you would have to generate the build command same way as generating the
environment.ts
file.Most enterprise angular applications I have worked on ends up extending the
webpack.config.js
file adding:This issue has been brought up for years with the Angular repo but i've never seen a resolution so I want to try to make it as clear as possible.
We should support a
dotenv()
style solution where in the code we can useprocess.env.VARIABLE
.In the build machine we can either:
export VARIABLE=test
or have a
.env
file to pick it up.This is supported by pretty much every other framework out there so I'm not sure why it wouldn't be possible or hasn't already been implemented.
If the issue is the angular team doesn't want this behaviour then that is fine but I am looking for a yes/no type answer. If the answer is no the angular team would be able to point to this issue for all future feature requests of this nature.
Describe the solution you'd like
No response
Describe alternatives you've considered
No response
The text was updated successfully, but these errors were encountered: