Skip to content

Provide a Way to Use Runtime Environment Variables #5309

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

Closed
tim273 opened this issue Mar 8, 2017 · 9 comments
Closed

Provide a Way to Use Runtime Environment Variables #5309

tim273 opened this issue Mar 8, 2017 · 9 comments

Comments

@tim273
Copy link

tim273 commented Mar 8, 2017

Currently angular-cli has the environment option, which works great for setting environment variables at build time, but I have a requirement to use environment variables at run time. So I am sent a header from an Apache server called 'X-Environment' which contains 'dev', 'qa', 'prod', etc, and then at I use that to determine which environment variables to use. The reasoning for this is that the build team at my work wants a single deployable file (what's in the dist folder zipped up) that is built once and and deployed to multiple environments. They also want to control environment properties as much as possible using CMDB and minimize having hard coded environment variables.

All the searching I've done provides solutions about setting environment variables at build time, but none at run time. It would be nice something were built in angular-cli to make this happen, rather than having to do it manually.

@AnnieCypcar
Copy link

+1

"ng eject" is available to run the server with webpack config, but the documentation for it is minimal.

@mattdistefano
Copy link

mattdistefano commented Mar 8, 2017

TBH I don't think is a CLI issue, because the CLI isn't a runtime tool. I would really look at refactoring your configuration to move those runtime variables out of the environment file and into something that can be set dynamically. Most likely you will need some server-side assist to do that since I don't think your angular app would have access to the request headers anyway. So for example you can have your web server inspect the header and set some global variables for the appropriate environment, then reference those variables in your angular app rather than using the environment files.

@tim273
Copy link
Author

tim273 commented Mar 8, 2017

What I mentioned above is doable because there are two other Angular 2 projects at my work doing it (using a header). They also had to set it up manually, but I can confirm it's working. Those projects used one of the angular-seed projects rather than angular-cli, so they use System.js and gulp, but I about to port over what they did to my project, which uses angular-cli.

@mattdistefano
Copy link

I'd be surprised if they're doing it entirely from the angular client-side code. Most likely the page that is loading the angular app contains server-side logic to inspect the header and vary some parameter in the rendered output.

@martinmcwhorter
Copy link

The safest runtime solution is to call http.get('/config.json') at startup.

@filipesilva
Copy link
Contributor

Well yeah, it's doable but you have to do it yourself. There's plenty of ways of getting config files at runtime. We added a build-time feature because that you couldn't code into your application.

@martinmcwhorter's suggestion is pretty simple and to the point.

Also related #3855 (comment)

@jared-christensen
Copy link

This article has a pretty good example of setting up a runtime solution for anyone stuck on this like I was. https://juristr.com/blog/2018/01/ng-app-runtime-config/

@alexpercsi
Copy link

For a framework that is very opinionated about pretty much everything, I'm surprised that there is no "Angular way" of doing this. I agree that this is probably not an angular-cli issue, but hitting a web server to load the config files at runtime feels hacky.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants