Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to have and read an environment file after an angular applicastion has been built

I’m building an angular application. This angular application will be delivered to multiple clients with differents needs and infrastructure. It will need several values(by example backend server, title, …) that should be configurable by the customer when doing the installation of this.

In debug, I can totally imagine those values coming from the environnments/environments.ts file, but once built, the environment.prod.ts cannot be changed anymore and therefore might not be built.

How can I provide values(I guess either by some file or environment variables) to the angular application(and how can I read them)?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It has not been decided yet, but most probably the built files will be wrapped in an nginx docker image.

>Solution :

Considering you want to first build your app, and then select a specific environment to deploy it, this tutorial may be of use to you. In summary you just have to follow 4 steps:

  1. Add a JSON configuration file in the src folder
  2. Update our angular/webpack configuration to include the file in our
    dist folder
  3. Add a simple configuration service with a call to get our config data from our config file
  4. Use APP_INITIALIZER to invoke the method retrieving our config data
    during the bootstrap process

If you follow those 4 steps, your configuration will be a JSON inside your dist folder (or whatever other outputPath you have in your angular.json file).

As for the docker part, you could add the config file directly inside the nginx container. Although I am guessing you would prefer to create a docker volume. So you not have to worry about copying the right config file for each client and instead just keep their specific config file in their servers.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading