<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Denis Hilt on Medium]]></title>
        <description><![CDATA[Stories by Denis Hilt on Medium]]></description>
        <link>https://medium.com/@dhilt?source=rss-586b0f6665d4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*xzcJHkkDscTo0SoE3Z2k8Q.jpeg</url>
            <title>Stories by Denis Hilt on Medium</title>
            <link>https://medium.com/@dhilt?source=rss-586b0f6665d4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 21:42:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dhilt/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to change Angular CLI Development Server Port via .env]]></title>
            <link>https://medium.com/@dhilt/how-to-change-angular-cli-development-server-port-via-env-9335d089e469?source=rss-586b0f6665d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/9335d089e469</guid>
            <category><![CDATA[angular-cli]]></category>
            <category><![CDATA[dotenv]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[packagejson]]></category>
            <category><![CDATA[env]]></category>
            <dc:creator><![CDATA[Denis Hilt]]></dc:creator>
            <pubDate>Mon, 04 May 2020 20:50:35 GMT</pubDate>
            <atom:updated>2020-05-04T21:08:04.589Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zxYAmOdrmYm7_fJ3q2xbsw.jpeg" /></figure><h3>How to change Angular CLI Development Server Port via .env</h3><p>There are different ways of setting up host and port development server parameters for Angular CLI:</p><ul><li>run dev server directly with command line params<br>ng serve --port 4201</li><li>add cmd params to npm script in package.json<br>&quot;start&quot;: &quot;ng serve --port 4201&quot;</li><li>update Angular project config (can change over time, this sample works for Angular 9)</li></ul><pre>&quot;projects&quot;: {<br>  &quot;my-angular-project&quot;: {<br>    &quot;architect&quot;: {  &quot;serve&quot;: { &quot;options&quot;: { &quot;port&quot;: 4201 } } }<br>  }<br>}</pre><h3>Multiple Angular App instances case</h3><p>But what if we need to get dev server port value from the environment variable? The use case might be as follows: multiple copies of the Angular application that should be synchronized with the repository and run on the same machine in parallel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rec0U1e_v7eCpIg2HsDERw.png" /></figure><p>Keeping all the copies synched with the repo means we can’t commit the value of the dev server port. Instead, this value should be a property of the app instance. This prohibits us from using package.json and Angular config options.</p><p>Yes, it is possible to run all the app instances directly via ng serve --port 42xx, but such processes must be as automated as possible. We should be able to set up “xx” only once per app and should not be obliged to keep an eye on these numbers while restarting our apps again and again.</p><h3>.env &amp; child_process solution</h3><p>Here we can use environment variables and <a href="https://www.npmjs.com/package/dotenv">dotenv</a><strong> </strong>is the common NodeJS tool designed for this purpose. It deals with <strong>.env</strong> file which stores our private data and which won’t be pushed to the repo.</p><p>The next problem is that we can’t assign Node environment variable as a value of the dev server port either in package.json or in Angular config json file. But we can run ng serve via standalone Node script that will parse <strong>.env </strong>file and use the result during Angular CLI command running.</p><p>Let’s create simple <strong>.env</strong> file with only 1 line setting up the name and the value of the dev server port environment variable:</p><pre>DEV_SERVER_PORT=4201</pre><p>Then we need to install <a href="https://www.npmjs.com/package/dotenv">dotenv</a> npm package (as development dependency) and define npm script that will carry out the remaining job by running some Node script, which we’ll implement shortly, let’s name it <strong>run-env</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d78cf3fa70dc0e828f37b580d1d1ecc/href">https://medium.com/media/8d78cf3fa70dc0e828f37b580d1d1ecc/href</a></iframe><p>Two things should be done by <strong>run-env </strong>script:</p><ul><li>access to DEV_SERVER_PORT variable through the .env</li><li>running ng serve with appropriate params</li></ul><p>The first issue is resolved with the help of <a href="https://www.npmjs.com/package/dotenv">dotenv </a>package. For the second issue a built-in <a href="https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback">child_process</a> NodeJS module could be used.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d38ccaa88acc1961fbbc7b089cf5a6de/href">https://medium.com/media/d38ccaa88acc1961fbbc7b089cf5a6de/href</a></iframe><h3>Conclusion</h3><p>No matter how many times and how many app instances we are going to run,npm start appears to be the only script we need to run with this approach. Just make sure you arranged .env file for each App instance you want to run on a specific port.</p><p>Full version of the gist: <a href="https://gist.github.com/dhilt/c47c50070b197b49adaa8abd6af04690">gist.github.com/dhilt/c47c50070b197b49adaa8abd6af04690</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9335d089e469" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>