How To Deploy Angular App On Heroku by@junaid-blogger

How To Deploy Angular App On Heroku

June 16th 2020 2,862 reads
Read on Terminal Reader
Open TLDR
react to story with heart
react to story with light
react to story with boat
react to story with money
How To Deploy Angular App On Heroku: How To deploy angular app on Heroku? We will talk about that how we can change our Angular app environment at run time when deploying the code. The solution for this is to have multiple environment file and choose one of them at the time of deployment dynamically. The next step is to create environment variable at Heroku. If you need help deploy angular webapp on heroku you can follow this: How-to-deploy-angular-6-app-on-heroku/
image
Junaid Blogger HackerNoon profile picture

Junaid Blogger

Junaid Raza is a digital marketing leader and tech blogger in Infoxen Technologies.

facebook social iconlinkedin social icontwitter social icongithub social icon

Hi All, Today we will talk about that how we can change our Angular app environment at run time when deploying the code on Heroku Server.

So, first we discuss that why we need this environment at run time. The situation is as described:

As normally every app is deployed in multiple environments e.g. staging, prod and service URL will be different for every environment. The solution for this is to have multiple environment file and choose one of them at the time of deployment dynamically.

Let’s take an example , you have this type of environment file:

image

This is your environment.prod.ts file and your have another api_url . So, will make another environment file as below:

image

We have named this file 

environment.test.ts
 and changed the
api_url 
to http://www.yahoo.com. So, now you have three environment file. One is for your local, one is prod and another is test.

Now, the next step is how would you choose your environment file at run time. The steps for this is given below:

1. Open the file 

angular.cli.json
. You will find your file like this:

image

We will add our new 

environment.test.ts
 file in environments section and then this section will we like:

image

2. So, first step is complete. Now, this is the time to move on next step and for that open your 

package.json
 file. You will find your file like this:

image

In the image you can see the 

postinstall
 section and replace this as below:

image

3. Our application code is now complete. The next step is to create environment variable at Heroku. Open Your app on heroku go to settings.

Then choose Reveal Config Vars . You can see the image:

image

In the end of the image, you can find KEY and VALUE section. We will put our environment var here as:

image

And then click on Add button to add the variable. Now the variable is added.

The process is complete. When you will deploy your app the

postinstall
command will run and will find the environment variable on heroku. Then your app will be deployed with environment.test.ts file.

If you need help deploy angular webapp on heroku you can follow this:
https://www.infoxen.com/blog/how-to-deploy-angular-6-app-on-heroku/

Previously published at https://medium.com/@abhishek.garg_53728/change-environment-at-runtime-in-angular-with-heroku-39478c3dfcd0


Disclaimer: The information expressed in the article belong solely to the author, and not necessarily to the author's employer, organization, committee or other group or individual.

react to story with heart
react to story with light
react to story with boat
react to story with money
L O A D I N G
. . . comments & more!