Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Angular schematics let you define repeatable changes to an Angular project, from creating files to configuring a library after installation. A schematic runs rules against a virtual file tree, so proposed changes can be staged before they are merged into the workspace. You can build a standalone collection for internal tooling or ship schematics with an Angular library.

How Angular schematics work

A schematic is an instruction for transforming a project. Its core pieces are a Tree, a Rule, and a collection. Angular describes a Rule as “a function that takes a Tree, applies transformations, and returns a new Tree.” The Tree represents the existing files and a staging area for proposed changes; the schematic framework applies those changes to the real workspace only when the tree is merged.

A Rule can stage file operations represented by these action types:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Create: add a file.
  • Rename: move or rename a file.
  • Overwrite: replace a file’s contents.
  • Delete: remove a file.

This virtual-tree workflow provides a boundary between a transformation and the actual project. The merge strategy determines whether staged changes are accepted, ignored, or cause an exception. See Angular’s schematic authoring guide for the Tree, Rule, and merge model.

Choose how to distribute the collection

Approach Best suited to How it is exposed
Standalone collection Internal tooling or transformations meant to be used independently of a library. Develop and package a collection using the Schematics CLI; users can invoke a named schematic from its collection.
Library-packaged collection Setup, generated artifacts, or migrations that should travel with a library. Keep schematic files in the library project, register them in collection.json, and expose the collection through the library package’s package.json.

Angular documents both approaches and recommends placing schematic files in the library project when the goal is to package them with that library. The library route ties initialization and future migrations to the package that needs them. See schematics for libraries.

Build a schematic step by step

  1. Create the collection structure. Use the Schematics CLI for a standalone collection, or add the schematic files to the library project. A collection is registered in collection.json.
  2. Register a named schematic. Add an entry to collection.json with a description and a factory path. The factory identifies the function Angular should run; an entry may also identify the option schema.
  3. Implement the rule factory. Write an entry function such as helloWorld(options): Rule that returns a Rule. The Rule receives a Tree, stages its transformations, and returns the Tree. Keep filesystem changes within this Tree-based workflow rather than making direct file edits.
  4. Define the options. Add a schema.json describing accepted inputs and defaults. Schemas can define strings, booleans, and enumerated choices. An x-prompt can request interactive input; schema constraints validate responses.
  5. Expose the collection if it belongs to a library. Set the library package’s schematics field to identify its collection so Angular CLI can discover the named entries.
  6. Review the staged result. Run the schematic in the intended workspace and inspect the proposed changes and merge behavior before relying on it for repeated use.

Angular’s library schematics documentation shows a collection entry with a factory such as ./ng-add/index#ngAdd and an associated schema. Treat these paths as documentation examples, not universal generated paths; confirm them against the Angular CLI and DevKit versions your library supports.

Choose the right CLI workflow

A library can use different schematic types for different lifecycle jobs. Choose the command based on what the transformation is meant to do:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
CLI workflow Purpose Typical schematic role
ng add Install or configure a package in a project. Initialize the library’s required configuration or dependencies.
ng generate Scaffold a project artifact. Create a component, service, or other library-specific structure.
ng update Adjust a project as a library changes across releases. Apply migration logic, including changes needed for breaking updates.

These are distinct tasks, not interchangeable labels: use an add schematic for setup, a generator for new artifacts, and an update schematic for release-related changes. Angular explains these library workflows in its library schematic guide.

Run a schematic through Angular CLI

Angular CLI’s default ng generate subcommands come from the @schematics/angular collection. A workspace can configure collections used for generation with the schematicCollections setting. You can also address a particular schematic in collection:name form. The exact command and package resolution depend on how the collection is installed and exposed; consult the Angular CLI generate reference and the collection’s documentation for the invocation appropriate to your setup.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check compatibility before publishing

Angular’s current documentation describes the authoring model, but its examples do not establish compatibility with every Angular CLI or DevKit release. Before publishing, check the Angular CLI and DevKit versions your library supports, then validate the collection metadata, factory paths, schemas, and generated changes in a workspace using those versions.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.