{"id":1968,"date":"2024-02-07T05:00:00","date_gmt":"2024-02-07T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=1968"},"modified":"2024-02-06T11:38:31","modified_gmt":"2024-02-06T19:38:31","slug":"all-about-environment-variables-with-angular","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/all-about-environment-variables-with-angular\/","title":{"rendered":"All about environment variables with Angular"},"content":{"rendered":"\n<p>Angular apps created with the Angular CLI used to have automatic environment creation built-in for new projects. While this automation was removed in recent years to simplify the learning curve of Angular, it is still possible to add support for different environments using the command:<\/p>\n\n\n\n<p><code>ng generate environments<\/code><\/p>\n\n\n\n<p>Before we generate such environments, it&#8217;s important to take a look at <code>angular.json<\/code> in the root folder of any Angular project, more specifically, the section called <strong>configurations<\/strong> in the <strong>build<\/strong> section:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"393\" height=\"340\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-9.png\" alt=\"\" class=\"wp-image-1970\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-9.png 393w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-9-300x260.png 300w\" sizes=\"auto, (max-width: 393px) 100vw, 393px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>This tells us that our project has two different configurations: <strong>production<\/strong> and <strong>development<\/strong>, and the default config for the <code>ng build<\/code> command is <strong>production<\/strong>.<\/p>\n\n\n\n<p>Note that the <strong>serve<\/strong> section of <code>angular.json<\/code> has a similar config that extends the build targets and overrides the default configuration by making it <strong>development<\/strong>, which, as a result, is what we use when we run <code>ng serve<\/code>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"577\" height=\"323\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-10.png\" alt=\"\" class=\"wp-image-1971\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-10.png 577w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-10-300x168.png 300w\" sizes=\"auto, (max-width: 577px) 100vw, 577px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>If we want to override the default configurations when running <code>ng serve<\/code> or <code>ng build<\/code>, we can do this by adding a configuration flag to the command and making it point to the proper configuration name, for instance:<\/p>\n\n\n\n<p><code>ng serve --configuration=production<\/code><\/p>\n\n\n\n<p><code>ng build --configuration=development<\/code><\/p>\n\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.angulartraining.com\/angular-certification.html\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"612\" height=\"368\" src=\"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/what-is-your-level.png\" alt=\"\" class=\"wp-image-1980\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/what-is-your-level.png 612w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/what-is-your-level-300x180.png 300w\" sizes=\"auto, (max-width: 612px) 100vw, 612px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do we plug environment variables into this?<\/h3>\n\n\n\n<p>When we run <code>ng generate environments<\/code>, the Angular CLI does two things:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"86\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-11.png\" alt=\"\" class=\"wp-image-1972\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-11.png 680w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-11-300x38.png 300w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>It creates two new files in <code>src\/environments<\/code>: <code>environments.ts<\/code> (the environment for <strong>production<\/strong>, which is also the default) and <code>environment.development.ts<\/code> for <strong>development<\/strong>.<\/p>\n\n\n\n<p><code>angular.json<\/code> gets updated with the following highlighted syntax:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"617\" height=\"315\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-12.png\" alt=\"\" class=\"wp-image-1973\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-12.png 617w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-12-300x153.png 300w\" sizes=\"auto, (max-width: 617px) 100vw, 617px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>As you can see, this instructs the builder to replace the production environment file with the development file when we use the <code>--configuration=development<\/code> flag.<\/p>\n\n\n\n<p>Such environment files can contain any environment variables we want. For instance, say I want to define a <code>SERVER_URL<\/code> that is different for production and development. I can do that:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"728\" height=\"174\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-13.png\" alt=\"\" class=\"wp-image-1974\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-13.png 728w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-13-300x72.png 300w\" sizes=\"auto, (max-width: 728px) 100vw, 728px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"636\" height=\"165\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-14.png\" alt=\"\" class=\"wp-image-1975\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-14.png 636w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-14-300x78.png 300w\" sizes=\"auto, (max-width: 636px) 100vw, 636px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Now, to use such environment variable in my code, all I need to do is import it as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"738\" height=\"238\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-15.png\" alt=\"\" class=\"wp-image-1976\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-15.png 738w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-15-300x97.png 300w\" sizes=\"auto, (max-width: 738px) 100vw, 738px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p><strong>Important:<\/strong> The import has to be from <code>\/environments\/environment<\/code>. Angular will pick the correct configuration based on the flag given to the <code>build<\/code> or <code>serve<\/code> command.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do you create environments for QA, pre-prod, and more?<\/h3>\n\n\n\n<p>Now that we know that angular.json is the file where all this magic happens, if we want to create a <strong>qa<\/strong> environment, we can head to the <strong>configurations <\/strong>section of <strong>build<\/strong>, add a new configuration block with the name q (or anything you want, of course), and make the file replacement to a new file that I call <strong><code>environment.qa.ts<\/code><\/strong>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"662\" height=\"374\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-16.png\" alt=\"\" class=\"wp-image-1978\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-16.png 662w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-16-300x169.png 300w\" sizes=\"auto, (max-width: 662px) 100vw, 662px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Then, I can create that environment file under <code>src\/environments<\/code> and add any variables I need in it:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"708\" height=\"161\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-17.png\" alt=\"\" class=\"wp-image-1979\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-17.png 708w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2024\/02\/image-17-300x68.png 300w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>And that&#8217;s it! We covered how to create environment variables for <strong>production<\/strong> and development and how to create other custom environments we might need.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Angular apps created with the Angular CLI used to have automatic environment creation built-in for new projects. While this automation was removed in recent years to simplify the learning curve of Angular, it is still possible to add support for different environments using the command: ng generate environments Before we generate such environments, it&#8217;s important [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,21],"tags":[],"class_list":["post-1968","post","type-post","status-publish","format-standard","hentry","category-angular","category-architecture"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=1968"}],"version-history":[{"count":4,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1968\/revisions"}],"predecessor-version":[{"id":1982,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/1968\/revisions\/1982"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=1968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=1968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=1968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}