{"id":414,"date":"2021-09-23T08:25:00","date_gmt":"2021-09-23T06:25:00","guid":{"rendered":"https:\/\/www.kehrwasser.com\/blog\/?p=414"},"modified":"2021-09-27T12:14:18","modified_gmt":"2021-09-27T10:14:18","slug":"low-level-configure-angular-apps-on-different-environemt-how-to","status":"publish","type":"post","link":"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/","title":{"rendered":"Low-Level: Configure Angular-Apps on different environments [How-To]"},"content":{"rendered":"<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>Disclaimer for german readers:<\/strong> Dieser Artikel besch\u00e4ftigt sich mit technischen Details. Falls sie auf der Suche nach Heigh-Level-Erkl\u00e4rungen sind, werden Sie in andern Artikeln eher f\u00fcndig.<\/p><\/blockquote>\n<p>It shouldn&#8217;t take a rebuild to reconfigure an app for a certain environment. It should be build once and in each deployed state (test, nightly, demo, pre-prod, staging, prod, etc) have the configuration that applies on the server, respectivly environment.<\/p>\n<p>The most common way to configure an app in a certain environment is by using environment variables. You can easily set them through .env files in Docker, Vagrant, AWS and other service providers.<\/p>\n<p>To archive that, let&#8217;s start at the server side: A JavaScript-application runs on a webserver. It consists out of plain text files. While running on a client&#8217;s browser, there is now way for the JavaScript to access server values such as environment variables during runtime.<\/p>\n<p>We need to inject the values, that life inside our server into the text files of our JavaScript application.<\/p>\n<p>First, prepare a file called <code>env.template.js<\/code> with the environment variables you expect.<\/p>\n<pre class=\"wp-block-preformatted\">(function (<em>window<\/em>) {\n    window[\"env\"] = window[\"env\"] || {};\n\n    <em>\/\/ Environment variables TEMPLATE<\/em>\n    window[\"env\"][\"title\"] = \"${TITLE}\"; \n})(<em>this<\/em>);<\/pre>\n<p>And again, there is no pre-existing mechanism. Here we can see, we prepare that file with placeholders <code>${XYZ}<\/code> as strings. To inject the environment variables, that live on the server and thus can be differ from one environment to another.<\/p>\n<p>To create a mechanism we need to run the following command on the server (this can be done during the deployment process to a certain environment, e.g. in an CI\/CD-pipeline.<\/p>\n<pre class=\"wp-block-preformatted\">envsubst < \/home\/nonroot\/htdocs\/assets\/env.template.js > \/home\/nonroot\/htdocs\/assets\/env.js<\/pre>\n<p>What this does is, it takes all the environment variables, set to a certain value currently on the machine, and replaces all the placeholders <code>${XYZ}<\/code> that match.<\/p>\n<p>For example if there was an environment variable <code>TITLE<\/code> set to <code>&quot;This is a title&quot;<\/code>, then any occurance of <code>${TITLE}<\/code> inside the file would be replaced by &#8222;This is a title&#8220; (without the quotes). After that the new resulting file is written into a new file, here <\/p>\n<pre id=\"block-97eedce0-723d-4231-bf96-8108d1e646da\" class=\"wp-block-preformatted\">\/home\/nonroot\/htdocs\/assets\/env.js<\/pre>\n<p>The contents of the new file would look like the following snippet shows.<\/p>\n<pre class=\"wp-block-preformatted\">(function (<em>window<\/em>) {\n    window[\"env\"] = window[\"env\"] || {};\n\n    <em>\/\/ Environment variables TEMPLATE<\/em>\n    window[\"env\"][\"title\"] = \"This is a title\"; \n})(<em>this<\/em>);<\/pre>\n<p>That is what we wanted to have. We had no pre-existing mechanism, that lets us access the environment variables on the server inside a certain environment. Now we have injected them very clean into our application.<\/p>\n<h4 class=\"wp-block-heading\">We have them available, how to process them in our application?<\/h4>\n<p>There is nothing wrong with simply including the <code>env.js<\/code> file in our <code>index.html<\/code>, right into the header (of course you can export and import it, which would be slightly cleaner).<\/p>\n<pre class=\"wp-block-preformatted\"><em><!-- Load environment variables --><\/em>\n<script src=\"assets\/env.js\"><\/script><\/pre>\n<p>Now, if the environemnt variable TITLE was declared on the machine the scripts are running on, <code>window[&#039;env&#039;][&#039;title&#039;]<\/code> should hold the value, in our case <code>&quot;This is a title&quot;<\/code> as string. Globaly, everywhere in our application (maybe some kind of anti-pattern, you have the force. Seal it with dependency injection and so on) Easy?<\/p>\n<h4 class=\"wp-block-heading\">Use the environment variable inside the environment files<\/h4>\n<p>The environment file in angular are a bit misleading. The implay, that you should have one file for each environment. I just look at <code>environment.ts<\/code> and <code>environment.prod.ts<\/code>. In the latter we can use our window[&#8222;env&#8220;] variable now.<\/p>\n<pre class=\"wp-block-preformatted\">export const environment = {\n  production: true,\n  title: window['env']['title'] || 'A default value for a title',\n};<\/pre>\n<p>That&#8217;s it. Comment if you like.<\/p>","protected":false},"excerpt":{"rendered":"<p>It shouldn&#8217;t take a rebuild to reconfigure an app for a certain environment. It should be build once and in each deployed state (test, nightly, demo, pre-prod, staging, prod, etc) have the configuration that applies on the server, respectivly environment.<\/p>","protected":false},"author":1,"featured_media":415,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[42],"tags":[120,121],"class_list":["post-414","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technologien","tag-angular","tag-code"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Low-Level: Configure Angular-Apps on different environments [How-To] - Looped Learning<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Low-Level: Configure Angular-Apps on different environments [How-To] - Looped Learning\" \/>\n<meta property=\"og:description\" content=\"It shouldn&#039;t take a rebuild to reconfigure an app for a certain environment. It should be build once and in each deployed state (test, nightly, demo, pre-prod, staging, prod, etc) have the configuration that applies on the server, respectivly environment.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/\" \/>\n<meta property=\"og:site_name\" content=\"Looped Learning\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-23T06:25:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-27T10:14:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/i2.wp.com\/www.kehrwasser.com\/blog\/wp-content\/uploads\/2021\/09\/angular-app-configuration-settings-different-environments.jpg?fit=1920%2C1280&ssl=1\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1280\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Kevin Heusinger\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Verfasst von\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kevin Heusinger\" \/>\n\t<meta name=\"twitter:label2\" content=\"Gesch\u00e4tzte Lesezeit\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\u00a0Minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/\",\"url\":\"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/\",\"name\":\"Low-Level: Configure Angular-Apps on different environments [How-To] - Looped Learning\",\"isPartOf\":{\"@id\":\"https:\/\/www.kehrwasser.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/i0.wp.com\/www.kehrwasser.com\/blog\/wp-content\/uploads\/2021\/09\/angular-app-configuration-settings-different-environments.jpg?fit=1920%2C1280&ssl=1\",\"datePublished\":\"2021-09-23T06:25:00+00:00\",\"dateModified\":\"2021-09-27T10:14:18+00:00\",\"author\":{\"@id\":\"https:\/\/www.kehrwasser.com\/blog\/#\/schema\/person\/d05f6ec3af9fdb4eb51b5aac1f9e91cf\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"de\",\"@id\":\"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/www.kehrwasser.com\/blog\/wp-content\/uploads\/2021\/09\/angular-app-configuration-settings-different-environments.jpg?fit=1920%2C1280&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/www.kehrwasser.com\/blog\/wp-content\/uploads\/2021\/09\/angular-app-configuration-settings-different-environments.jpg?fit=1920%2C1280&ssl=1\",\"width\":1920,\"height\":1280},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Startseite\",\"item\":\"https:\/\/www.kehrwasser.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Low-Level: Configure Angular-Apps on different environments [How-To]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.kehrwasser.com\/blog\/#website\",\"url\":\"https:\/\/www.kehrwasser.com\/blog\/\",\"name\":\"Looped Learning\",\"description\":\"#innovation, #ki, #newWork, #agile, #validatedLearning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.kehrwasser.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"de\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.kehrwasser.com\/blog\/#\/schema\/person\/d05f6ec3af9fdb4eb51b5aac1f9e91cf\",\"name\":\"Kevin Heusinger\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"de\",\"@id\":\"https:\/\/www.kehrwasser.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/99ba53c67cb33396656b9736e215b87b954fd3c27c0e38ddc100f885da526bfb?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/99ba53c67cb33396656b9736e215b87b954fd3c27c0e38ddc100f885da526bfb?s=96&d=mm&r=g\",\"caption\":\"Kevin Heusinger\"},\"sameAs\":[\"https:\/\/www.kehrwasser.com\"],\"url\":\"https:\/\/www.kehrwasser.com\/blog\/author\/kw-kevin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Low-Level: Configure Angular-Apps on different environments [How-To] - Looped Learning","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/","og_locale":"de_DE","og_type":"article","og_title":"Low-Level: Configure Angular-Apps on different environments [How-To] - Looped Learning","og_description":"It shouldn't take a rebuild to reconfigure an app for a certain environment. It should be build once and in each deployed state (test, nightly, demo, pre-prod, staging, prod, etc) have the configuration that applies on the server, respectivly environment.","og_url":"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/","og_site_name":"Looped Learning","article_published_time":"2021-09-23T06:25:00+00:00","article_modified_time":"2021-09-27T10:14:18+00:00","og_image":[{"width":1920,"height":1280,"url":"https:\/\/i2.wp.com\/www.kehrwasser.com\/blog\/wp-content\/uploads\/2021\/09\/angular-app-configuration-settings-different-environments.jpg?fit=1920%2C1280&ssl=1","type":"image\/jpeg"}],"author":"Kevin Heusinger","twitter_card":"summary_large_image","twitter_misc":{"Verfasst von":"Kevin Heusinger","Gesch\u00e4tzte Lesezeit":"3\u00a0Minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/","url":"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/","name":"Low-Level: Configure Angular-Apps on different environments [How-To] - Looped Learning","isPartOf":{"@id":"https:\/\/www.kehrwasser.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/#primaryimage"},"image":{"@id":"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/www.kehrwasser.com\/blog\/wp-content\/uploads\/2021\/09\/angular-app-configuration-settings-different-environments.jpg?fit=1920%2C1280&ssl=1","datePublished":"2021-09-23T06:25:00+00:00","dateModified":"2021-09-27T10:14:18+00:00","author":{"@id":"https:\/\/www.kehrwasser.com\/blog\/#\/schema\/person\/d05f6ec3af9fdb4eb51b5aac1f9e91cf"},"breadcrumb":{"@id":"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/"]}]},{"@type":"ImageObject","inLanguage":"de","@id":"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/#primaryimage","url":"https:\/\/i0.wp.com\/www.kehrwasser.com\/blog\/wp-content\/uploads\/2021\/09\/angular-app-configuration-settings-different-environments.jpg?fit=1920%2C1280&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.kehrwasser.com\/blog\/wp-content\/uploads\/2021\/09\/angular-app-configuration-settings-different-environments.jpg?fit=1920%2C1280&ssl=1","width":1920,"height":1280},{"@type":"BreadcrumbList","@id":"https:\/\/www.kehrwasser.com\/blog\/2021\/09\/23\/low-level-configure-angular-apps-on-different-environemt-how-to\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Startseite","item":"https:\/\/www.kehrwasser.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Low-Level: Configure Angular-Apps on different environments [How-To]"}]},{"@type":"WebSite","@id":"https:\/\/www.kehrwasser.com\/blog\/#website","url":"https:\/\/www.kehrwasser.com\/blog\/","name":"Looped Learning","description":"#innovation, #ki, #newWork, #agile, #validatedLearning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kehrwasser.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"de"},{"@type":"Person","@id":"https:\/\/www.kehrwasser.com\/blog\/#\/schema\/person\/d05f6ec3af9fdb4eb51b5aac1f9e91cf","name":"Kevin Heusinger","image":{"@type":"ImageObject","inLanguage":"de","@id":"https:\/\/www.kehrwasser.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/99ba53c67cb33396656b9736e215b87b954fd3c27c0e38ddc100f885da526bfb?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/99ba53c67cb33396656b9736e215b87b954fd3c27c0e38ddc100f885da526bfb?s=96&d=mm&r=g","caption":"Kevin Heusinger"},"sameAs":["https:\/\/www.kehrwasser.com"],"url":"https:\/\/www.kehrwasser.com\/blog\/author\/kw-kevin\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.kehrwasser.com\/blog\/wp-content\/uploads\/2021\/09\/angular-app-configuration-settings-different-environments.jpg?fit=1920%2C1280&ssl=1","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.kehrwasser.com\/blog\/wp-json\/wp\/v2\/posts\/414","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kehrwasser.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kehrwasser.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kehrwasser.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kehrwasser.com\/blog\/wp-json\/wp\/v2\/comments?post=414"}],"version-history":[{"count":5,"href":"https:\/\/www.kehrwasser.com\/blog\/wp-json\/wp\/v2\/posts\/414\/revisions"}],"predecessor-version":[{"id":424,"href":"https:\/\/www.kehrwasser.com\/blog\/wp-json\/wp\/v2\/posts\/414\/revisions\/424"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kehrwasser.com\/blog\/wp-json\/wp\/v2\/media\/415"}],"wp:attachment":[{"href":"https:\/\/www.kehrwasser.com\/blog\/wp-json\/wp\/v2\/media?parent=414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kehrwasser.com\/blog\/wp-json\/wp\/v2\/categories?post=414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kehrwasser.com\/blog\/wp-json\/wp\/v2\/tags?post=414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}