Sub topic 17
Schematics and Builders: How the CLI Is Extended
This chapter assumes you can read angular.json, run ng generate and ng build, and know what an npm package is. It explains the two extension mechanisms behind those commands. A schematic changes your source files. A builder runs a task such as building or testing.
Two kinds of extension
Every CLI command you have used belongs to one of two families.
- Commands that change files in your workspace use schematics.
ng generate,ng addandng updateare in this family. - Commands that run a task on a project use builders.
ng build,ng serveandng testare in this family.
Neither family is hard-coded into the ng binary. Both are packages that the CLI loads by name. That is why a third-party library can add its own ng add step or its own build target.
| Aspect | Schematic | Builder |
|---|---|---|
| Purpose | Create or modify files | Run a task (build, serve, test, lint) |
| Invoked by | `ng generate`, `ng add`, `ng update`, `ng new` | `ng build`, `ng serve`, `ng test`, `ng run` |
| Configured in | `schematics` section of `angular.json`, and the package's `collection.json` | `architect` section of `angular.json`, and the package's `builders.json` |
| Input | Options and the current file tree | Options and a build context |
| Output | A set of file changes | Files on disk, a dev server, test results |
Schematics
A schematic is a function that receives a virtual file tree and returns a changed tree. The CLI applies the changes to disk only after the whole schematic succeeds. If a schematic throws an error halfway, nothing is written.
A group of schematics is a collection. A collection is described by a collection.json file inside an npm package. When you run ng generate component, the CLI looks in the default collection, @schematics/angular, finds the schematic named component and runs it with your options.
You can name another collection explicitly, using the collection:schematic form.
ng generate @schematics/angular:component products/product-list
ng generate @schematics/angular:service products/productThese two commands do the same thing as ng generate component and ng generate service. The short forms work because @schematics/angular is the default collection.
Dry runs
Before you generate code, you can preview what a schematic would do. The --dry-run flag prints the files that would be created or changed and writes nothing.
ng generate component products/product-list --dry-runUse it whenever you are unsure which folder a schematic will write to, or when you try an unfamiliar third-party schematic.
Setting schematic defaults in angular.json
The schematics section of angular.json sets default options for schematics. This avoids repeating flags on every command. The excerpt below makes every generated component use OnPush change detection and skip test files.
{
"projects": {
"my-app": {
"projectType": "application",
"root": "",
"sourceRoot": "src",
"schematics": {
"@schematics/angular:component": {
"changeDetection": "OnPush",
"skipTests": true
}
}
}
}
}The key is the full collection:schematic name, and the value is an object of that schematic's options. You can place the same block at the workspace level, outside projects, to apply it to every project. A project-level block overrides the workspace-level one.
How ng add and ng update use schematics
ng add installs a package and then runs its ng-add schematic. That schematic does the setup work: it edits app.config.ts, adds styles to angular.json, or changes other files. ng update runs migration schematics. A library lists these in its package.json under ng-update, and each one rewrites code that would break across a version change. The two commands show that a library can ship both a runtime and the tooling that installs and upgrades it.
ng add @angular/material
ng update @angular/core @angular/cliBuilders
A builder is a function that the CLI runs for a target. A target is a named task on a project, such as build or test. Targets live in the architect section of angular.json. Each target has a builder, shared options, and optional configurations.
{
"projects": {
"my-app": {
"architect": {
"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 value has the form package:builder-name. Here the package is @angular/build and the builder is application. Older projects, roughly Angular 17 to 19, use @angular-devkit/build-angular:application for the same job. If you open an older workspace and see that name, it is the same idea under a different package.
The ng build command is a shortcut for running the build target. The CLI reads the target, loads the named builder, merges options with the chosen configuration, validates the result against the builder's schema, and calls the builder.
ng build --configuration developmentTargets are not limited to the built-in names
The names build, serve and test are only conventions that have shortcut commands. You can define any target and run it with ng run, using the form project:target[:configuration].
ng run my-app:build:developmentThis command is equivalent to the ng build line above. With ng run, a custom target works the same way.
Builder options are validated
Each builder ships a JSON schema that lists its allowed options, their types and their defaults. This is why a typo in angular.json, such as "sourcemap" instead of "sourceMap", produces an error message that names the bad property instead of failing silently. It is also how editors offer autocompletion inside angular.json.
How the pieces fit together
The CLI reads your command, picks the right extension type, loads the package, and runs it.
Common errors
- Schematic not found. The collection is not installed, or you misspelled its name. Install the package first, or use
ng addso the setup runs for you. - Builder not found. The package named in
builderis missing fromnode_modules, or you changed Angular versions and the builder package was renamed. Runnpm install, then compare the name with a freshly generated workspace. - Option rejected by the schema. The builder does not recognize a property. Check the builder's documented options and the spelling.
- Defaults ignored. You put a schematic default under the wrong key. The key must be the full
collection:schematicname, for example@schematics/angular:component.
When to write your own
Most teams never write a builder. Writing a schematic is more common, for example to generate a feature folder that follows your team's conventions. Consider a custom extension when a repeated manual step is error-prone, and a shell script or npm script is not enough. For one-off tasks, an npm script is simpler and easier to maintain.
Exercise
You want every component generated in your project to use OnPush change detection. Which section of angular.json do you edit, and what is the key you use?
Show answer
Edit the schematics section, at the project or workspace level. The key is the full schematic name, @schematics/angular:component, and its value is an object containing "changeDetection": "OnPush".
Exercise
What does the builder value @angular/build:application mean, and how would you run the build target with the development configuration without using ng build?
Show answer
It names the application builder in the @angular/build package. Run ng run my-app:build:development, where my-app is the project name.