Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

How to have various environments with Angular CLI future (Angular 7)

Author: Sagar Rollins
by Sagar Rollins
Posted: Oct 25, 2022

Most web applications require to run in different environments before they make their way to production. You might need a build for your favorite QA team to perform some special tests, or a specific build to run on your continuous integration servers for instance.

As a result of this, we would need builds of different configurations: Different server URLs, different constants, different logging options, etc.

Angular CLI offers an environment feature that allows running builds targeted at specific environments for specific requirements. For instance, here is how you would run a build for production:

ng build — env=prod // up to Angular 5

Update: With Angular 6 and Above, the command is now:

ng build — configuration=production // new versions

The prod flag in the above code refers to the prod (production in v6 and above) property of the environments section of.angular-cli.json before v6:(now angular.json in v6 and above), which has two options by default: dev and prod

"environments": {

"dev": "environments/environment.ts",

"prod": "environments/environment.prod.ts"

}

You can add as many environments as you need here and name them accordingly. For instance, if you need a assesment build option, just add the following entry in.angular-cli.json:

"environments": {

"dev": "environments/environment.ts",

"prod": "environments/environment.prod.ts",

"qa": "environments/environment.qa.ts"

}

For v6 and above, angular.json environments are now called configurations. Here is how to add a new qa environment after v6:

"configurations": {

"production": { … },

"qa": {

"fileReplacements": [

{

"replace": "src/environments/environment.ts",

"with": "src/environments/environment.qa.ts"

}

]

}

}

Then you have to create the actual file environment.qa.ts in the environments directory.

Here is what the default environment.ts file for dev looks like:

// The file contents for the current environment will overwrite these during build.

// The build system defaults to the dev environment which uses `environment.ts`, but if you do

// `ng build — env=prod` then `environment.prod.ts` will be used instead.

// The list of which env maps to which file can be found in `.angular-cli.json`.

export const environment = {

production: false

};

The above environment object is where you would add any environment specific property. For instance, let’s add a server URL:

export const environment = {

production: false,

serverUrl: "http://dev.server.companyName.com"

};

Then all you have to do to provide a different URL for QA is to define that same property with the right value in environment.qa.ts:

export const environment = {

production: false,

serverUrl: "http://qa.server.companyName.com"

};

Now that your environments are defined, how do you use those properties in your code? Easy enough, all you have to do is import the environmentobject as follows:

import {environment} from ‘../../environments/environment’;

@Injectable()

export class AuthService { API_URL: string = environment.serverUrl + ‘/theUrlForAPI’ ;

Then when you run a build for QA, Angular CLI is going to use environment.qa.ts to read the environment.serverUrl property value and you’re all set to deploy that build to the QA environment.

My Name is Prateek Pandey. I am a Developer, as well as consultant and trainer for Angular where I help web developors learn and ease the stuff.

For more information click here

About the Author

We provide end-to-end product development services. We help you shape the perfect strategies, satisfying your goal with innovative ideas and thus empowering your business with the best tech solutions.

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
  • Guest  -  2 years ago

    If you want to know how to catch a cheating spouse you are in luck because the options are endless. There are many different ways to go about catching a cheating partner, such as hiring a private investigator going through their social media account, or planting a recording device or GPS tracker on their device. However, instead of wasting money on buying an expensive recording device or GPS tracker, you can simply go through your partner phone with the help of ( infotheprohackers @ gmail.com,) He can grant you access to your spouse\'s phone, records track their location and access social media accounts all at once, you can conveniently access all the details from one single place on a web-based dashboard. So if you want to end your cheating husband’s infidelity, you can choose infotheprohackers @ gmail.com or contact him signal or Telegram +1(341)465-4599) to find all the evidence needed to catch a cheater.

Author: Sagar Rollins

Sagar Rollins

Member since: Oct 22, 2022
Published articles: 2

Related Articles