Sub topic 12
Build configurations and environment-specific settings
This chapter assumes you can read angular.json (see the previous chapters on the workspace and ng build) and know basic TypeScript imports.
What a build configuration is
A build configuration is a named set of option overrides for a target such as build or serve. The target's options block holds settings shared by every build. Each entry under configurations is merged on top of those options when you select it.
"build": {
"builder": "@angular/build:application",
"options": {
"browser": "src/main.ts",
"tsConfig": "tsconfig.app.json"
},
"configurations": {
"production": {
"outputHashing": "all"
},
"development": {
"optimization": false,
"sourceMap": true
}
},
"defaultConfiguration": "production"
}The builder package name depends on your version. Angular 17 to 19 projects use @angular-devkit/build-angular:application.
Choose a configuration with the --configuration flag. If you omit it, the CLI uses defaultConfiguration.
ng build --configuration development
ng buildThe second command builds with production.
Environment files
Environment-specific values, such as an API URL, live in files under src/environments. Newer workspaces do not create them by default. Generate them with:
ng generate environmentsThis creates environment.ts and environment.development.ts, and adds a fileReplacements entry to the development configuration.
// src/environments/environment.ts (used by production)
export const environment = {
production: true,
apiUrl: 'https://api.example.com',
};// src/environments/environment.development.ts
export const environment = {
production: false,
apiUrl: 'http://localhost:3000',
};The fileReplacements entry tells the build to swap one file for another at compile time.
"development": {
"optimization": false,
"sourceMap": true,
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.development.ts"
}
]
}Your code always imports the base file. The build decides which content you get.
import { Injectable } from '@angular/core';
import { environment } from '../environments/environment';
@Injectable({ providedIn: 'root' })
export class ApiConfig {
readonly baseUrl = environment.apiUrl;
}Adding your own configuration
To add a staging build, add a configuration with its own replacement file, then run it.
"staging": {
"outputHashing": "all",
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.staging.ts"
}
]
}ng build --configuration stagingng serve has its own configurations block. It usually points each entry at the matching build configuration through buildTarget, such as my-app:build:development.
Common errors
- Wrong configuration name: the CLI reports that the configuration is not set in
angular.json. Check the spelling against theconfigurationskeys. - Importing the environment file directly: importing
environment.developmentin code defeats the swap, so the value is fixed in every build. Always importenvironment. - Secrets in environment files: the values are compiled into the JavaScript sent to the browser. Never put private keys there.
Exercise
You run ng build with no flags and the bundle contains the development API URL. Which two places in angular.json would you check?
Show answer
Check defaultConfiguration on the build target, because it may be set to development. Also check whether the fileReplacements entry sits in the options block instead of only in the development configuration, since shared options apply to every build.