Angular allows us to build Angular applications for different environments using the ng build
command.
Suppose we have two environment files: environment.dev.ts
and environment.prod.ts
. We want to build our Angular application using the environment file for the development environment: environment.dev.ts
.
In our angular.json
file, we add the following in the development
object within the configurations
object in order to tell Angular to use the environment.dev.ts
file:
"configurations": {
"development": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.dev.ts"
}
]
}
}
Now, we can run the following command:
ng build --configuration=development
.
If you are using Angular 12+, you can find the default configuration when the build
command is executed in the build
object with the key defaultConfiguration
in angular.json
.
We can make this process a little simpler by just adding the suffix --configuration=
along with the environment that we want in package.json
in the scripts
object for build
. Instead of just ng build
, we can tell ng build
that we want to build for using the production environment file ng build --configuration=production
.
"scripts": {
"build": "ng build --configuration=production"
}
Now, we can run ng build
using npm
: npm build
.