Sub topic 12
Build configurations and environment-specific settings
This chapter assumes you can read angular.json at the level of the previous chapters. You should know that a project has architect targets such as build and serve, and each target names a builder and a set of options. Here you will see how one build target produces different outputs for development, production, and any other environment you define.
What a configuration is
A configuration is a named set of option overrides for a target. The target's options object holds values shared by every build. Each entry under configurations changes only the options it lists. The defaultConfiguration entry says which one applies when you pass no flag.
{
"projects": {
"my-app": {
"architect": {
"build": {
"builder": "@angular/build:application",
"options": {
"browser": "src/main.ts",
"tsConfig": "tsconfig.app.json",
"styles": ["src/styles.css"]
},
"configurations": {
"production": {
"outputHashing": "all",
"budgets": [
{ "type": "initial", "maximumWarning": "500kB", "maximumError": "1MB" }
]
},
"development": {
"optimization": false,
"sourceMap": true,
"extractLicenses": false
}
},
"defaultConfiguration": "production"
}
}
}
}
}The builder name depends on your CLI version. Angular 17 to 19 projects often show @angular-devkit/build-angular:application. Newer projects use @angular/build:application. The configuration mechanism is the same for both.
How the CLI resolves options
The CLI builds the final option set in layers. It starts with options, applies the chosen configuration on top, and then applies any flags you typed.
Because the diagram fence must follow the platform format, here it is in the required form:
Two consequences follow from this order:
- A configuration never needs to repeat shared settings such as
tsConfig. It lists only what differs. - A flag always wins.
ng build --configuration production --source-mapproduces source maps even though the production configuration leaves them off.
Choosing a configuration
Select a configuration with --configuration or the short form -c.
ng build # uses defaultConfiguration
ng build --configuration development
ng build -c production
ng build -c production,staging # comma list, later entries override earlier onesng serve has its own defaultConfiguration, normally development. Each serve configuration points at a build configuration through buildTarget. Projects created before Angular 17 used the key browserTarget for the same purpose.
"serve": {
"builder": "@angular/build:dev-server",
"configurations": {
"production": { "buildTarget": "my-app:build:production" },
"development": { "buildTarget": "my-app:build:development" }
},
"defaultConfiguration": "development"
}The string my-app:build:production reads as project, target, configuration. Running ng serve -c production therefore serves an optimized build.
What production and development change
The two standard configurations differ in a few options that matter in daily work.
| Option | Production | Development |
|---|---|---|
| `optimization` | On (minify, tree-shake, inline critical CSS) | Off |
| `sourceMap` | Off | On |
| `outputHashing` | `all` (hash in file names for cache busting) | `none` |
| `extractLicenses` | On | Off |
| `budgets` | Enforced | Not set |
Development builds are larger and slower at runtime, but they rebuild fast and give readable stack traces. Never measure performance or ship from a development build.
Environment files and file replacement
Applications often need values that differ per environment, such as an API base URL. The CLI handles this with file replacement. At build time it swaps one source file for another, so your code always imports the same path.
Newer workspaces do not include environment files by default. Generate them with the CLI:
ng generate environmentsThis creates src/environments/environment.ts and src/environments/environment.development.ts. It also adds a fileReplacements entry to the development configuration. The base file is the production one, and the development file replaces it only in the development build.
// src/environments/environment.ts (production values)
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 generated entry in angular.json looks like this:
"development": {
"optimization": false,
"sourceMap": true,
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.development.ts"
}
]
}Application code imports the base file and never names a specific environment:
import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from '../environments/environment';
@Injectable({ providedIn: 'root' })
export class ProductService {
private readonly http = inject(HttpClient);
load() {
return this.http.get(`${environment.apiUrl}/products`);
}
}This service needs provideHttpClient() in the providers of app.config.ts to run.
Adding a custom configuration
To add a staging environment, create src/environments/environment.staging.ts with the same exported shape. Then add a configuration that replaces the base file with it.
"staging": {
"optimization": true,
"outputHashing": "all",
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.staging.ts"
}
]
}If you want ng serve -c staging, also add a matching entry under the serve target with "buildTarget": "my-app:build:staging". Build with ng build -c staging.
Budgets
A budget sets a size limit that the build checks. If a bundle crosses maximumWarning, the build prints a warning. If it crosses maximumError, the build fails. Common types are initial (the code loaded at startup), anyComponentStyle (a single component's styles), and bundle. Failing the build on size is useful in a CI pipeline, because it catches a heavy new dependency before release.
Common errors
- Wrong configuration name.
ng build -c prodfails ifangular.jsondefinesproduction. The CLI reports that the configuration is not set in the workspace. - Editing only one target. If you add a
stagingbuild configuration but noserveentry,ng serve -c stagingfails because the serve target has no such configuration. - Replacing a file that does not match. The
replacepath is relative to the workspace root and must match the imported file. A typo means no replacement occurs, and the build silently uses the wrong values. - Different export shapes. Every environment file must export the same properties. A property missing from the staging file passes through as a type error only where the code uses it, so keep the files in sync.
- Forgetting that flags win. A leftover flag in an npm script can override your configuration without any warning.
When build-time configuration is not enough
File replacement fixes values at build time, so each environment needs its own build. If you want one artifact that you promote from staging to production, load settings at runtime instead. A common pattern is to serve a config.json from public/ or assets/, fetch it before startup, and provide it through dependency injection. The trade-off is an extra request at startup and a small amount of code.
Exercise
Your team has development and production configurations. You add staging under build with a fileReplacements entry, then run ng serve -c staging and get an error. What did you forget, and what value should the new entry have?
Show answer
You forgot to add a staging configuration under the serve target. Add it with "buildTarget": "my-app:build:staging" (use your own project name in place of my-app). The serve target only knows configurations that it defines, and each one points to a build configuration.
Exercise
A production build uses outputHashing: "all" and you run ng build -c production --output-hashing none. Which value is used, and why?
Show answer
none is used. Command-line flags are applied last, after the target options and the selected configuration, so they override both.