Sub topic 8
Reading angular.json
angular.json is the workspace configuration file. It tells the CLI which projects exist, which tasks each project supports, and which options each task uses. This chapter assumes you have created a workspace with ng new and know what main.ts and tsconfig.json do.
The top-level structure
A generated file has a few top-level keys.
| Key | Purpose |
|---|---|
| `$schema` | Points editors to a JSON schema, which gives autocomplete and validation. |
| `version` | The file format version, currently `1`. |
| `newProjectRoot` | The folder where `ng generate application` and `ng generate library` place new projects, usually `projects`. |
| `projects` | An object with one entry per application or library. |
| `cli` | Optional workspace-wide CLI settings, such as the package manager or analytics. |
| `schematics` | Optional defaults for `ng generate`, covered below. |
Everything else lives under projects. A single-application workspace has one entry there, named after the workspace.
A project entry
The following excerpt shows one project with its main targets. Angular 17 to 19 use the builder prefix @angular-devkit/build-angular. Newer versions use @angular/build, as shown here.
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"my-app": {
"projectType": "application",
"root": "",
"sourceRoot": "src",
"prefix": "app",
"schematics": {
"@schematics/angular:component": { "style": "scss" }
},
"architect": {
"build": {
"builder": "@angular/build:application",
"options": {
"browser": "src/main.ts",
"tsConfig": "tsconfig.app.json",
"assets": [{ "glob": "**/*", "input": "public" }],
"styles": ["src/styles.scss"]
},
"configurations": {
"production": {
"budgets": [
{ "type": "initial", "maximumWarning": "500kB", "maximumError": "1MB" }
],
"outputHashing": "all"
},
"development": { "optimization": false, "sourceMap": true }
},
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular/build:dev-server",
"configurations": {
"production": { "buildTarget": "my-app:build:production" },
"development": { "buildTarget": "my-app:build:development" }
},
"defaultConfiguration": "development"
},
"test": {
"builder": "@angular/build:karma",
"options": { "tsConfig": "tsconfig.spec.json" }
}
}
}
}
}The project-level fields describe where the project lives.
projectTypeisapplicationorlibrary. It changes which targets and generators make sense.rootis the project's folder relative to the workspace. An empty string means the workspace root itself.sourceRootis where the CLI looks for source files, andng generatewrites new files under it.prefixis prepended to component selectors. Withapp, a generatedproduct-listcomponent gets the selectorapp-product-list.
Targets and builders
The architect object (called targets in some tools) maps target names to builders. A target is a named task, such as build, serve or test. A builder is the code that performs the task. The builder string has the form package:builder-name.
The target name is just a label. ng build runs the target named build. You could add a target named lint that uses a linting builder and run it with ng lint.
Since the diagram must use the proper fence, here is the correct version:
How options are merged
Each target has an options object and an optional configurations object. When you run a target, the CLI builds the final settings in this order, with later steps overriding earlier ones:
- The target's
options. - The selected configuration, if any.
- Flags you pass on the command line.
Take ng build --configuration development --source-map=false as an example. It starts with the shared options, applies optimization: false and sourceMap: true from the development configuration, and then the flag turns source maps off again.
defaultConfiguration decides which configuration applies when you pass none. In the example above, ng build uses production and ng serve uses development.
Common options in the build target
browseris the entry file, normallysrc/main.ts. Server-side rendering addsserverandssrkeys.tsConfigis the TypeScript configuration used for this target. The build usestsconfig.app.jsonand the test target usestsconfig.spec.json.assetslists files copied to the output as-is, such as images and the favicon. An entry is either a path string or an object withglob,inputand optionallyoutput.styleslists global stylesheets. Component styles are not listed here.budgetssets size limits. ExceedingmaximumWarningprints a warning. ExceedingmaximumErrorfails the build, so a regression in bundle size is caught in CI.outputHashingadds a content hash to file names so browsers can cache files safely.
The serve and test targets
The serve target does not repeat build settings. Its buildTarget value, my-app:build:development, follows the pattern project:target:configuration. The dev server runs that build and serves the result. If you add a file to the build assets, ng serve picks it up without any change to the serve target.
The same project:target:configuration pattern is used with the lower-level ng run command. ng run my-app:build:development does the same thing as ng build --configuration development.
The test target points to a test builder and a tsConfig. Its details are covered in the chapter on running tests.
Changing settings safely
You can edit the file by hand, and the $schema entry means most editors flag typos. The CLI also provides ng config.
ng config projects.my-app.architect.build.options.styles
ng config projects.my-app.prefix acme
ng config cli.packageManager pnpmWith one argument, ng config prints the value. With two, it writes the value. This is useful in scripts, and it preserves the file's formatting.
Per-project schematics defaults, like the style: scss entry above, change what ng generate produces. Setting "@schematics/angular:component": { "changeDetection": "OnPush" } makes every new component use OnPush without extra flags.
Common mistakes
- Editing
outputPathorstylesin a configuration and expecting arrays to merge. A configuration replaces an array value rather than appending to it, so repeat every entry you want to keep. - Pointing
buildTargetat a configuration that does not exist, which fails with an error naming the missing configuration. - Copying a builder name from an older tutorial. Use the package that matches your Angular version, or run
ng update, which migrates builder names for you. - Putting a path in
assetsthat does not exist. The build fails instead of ignoring it.
Exercise
A serve target has "defaultConfiguration": "development" and the build target's development configuration sets "sourceMap": true. You run ng serve --source-map=false. Which value is used, and why?
Show answer
Source maps are off. Command-line flags are applied last, after the target options and the selected configuration, so --source-map=false overrides the true set in the development configuration.
Exercise
What does the value my-app:build:production mean, and where does it appear in angular.json?
Show answer
It names the project my-app, the target build and the configuration production. It appears as the buildTarget value in the serve target's configurations, and the same form is used on the command line with ng run.