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

To add schematics to an Angular library, package a schematic collection with the library, register an ng-add schematic for installation-time setup, and add named generation or update schematics for other tasks. Each schematic maps to a factory and may define its command options in a JSON schema. Build and include the schematic files in the distributable, then test the installed package from a representative Angular workspace.

What Angular library schematics do

A schematic is a template-based code generator that supports complex logic, according to Angular’s schematics overview. For a library, schematics let its package participate in Angular CLI workflows:

  • ng add installs and configures the library in a consumer’s project.
  • ng generate creates library-specific files or changes project configuration.
  • ng update migrates a consumer’s project when a library release changes dependencies, configuration, or code.

Use them when setup or generation needs meaningful customization or must alter the consumer’s workspace. If the desired result is fixed and only lightly customizable, a runtime feature such as a dynamic component may be a better fit; a schematic generates files into the project, while a runtime feature creates behavior when the application runs. Angular discusses this distinction in its library schematics guide.

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

How the collection and package fit together

A library’s collection.json is its schematic index. It maps names such as ng-add or my-service to a factory and, when applicable, an option schema. The package metadata points Angular CLI to that collection. A consumer can then invoke a named schematic with a command such as ng generate my-lib:my-service.

The collection is only one part of the package. The library also needs schematic source files, a separate schematic build configured for the project, and the compiled schematic output included in the published package. Follow the library guide’s packaging pattern, checking its build configuration and package metadata against the Angular and DevKit versions your library targets.

Choose a schematic for each CLI task

Installation setup with ng add

Name the installation schematic ng-add in the collection. It can make project-level changes after the package manager installs the library—for example, adding setup that a consumer would otherwise have to perform manually. The package can configure whether installation saves it as a dependency or a development dependency; choose based on how the library is used rather than assuming one setting fits every package.

Library-specific generation with ng generate

A named generation schematic can scaffold files or update workspace configuration. Its factory receives command options and returns rules that transform the consumer’s project. Where the workspace has multiple projects, use the intended project information rather than assuming there is only one application or library.

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

Define accepted command options and defaults in the schematic’s JSON schema. Angular CLI uses that schema to understand which options a command accepts; keep the schema aligned with the factory’s behavior so consumers get predictable validation and defaults.

Release migrations with ng update

Attach update schematics to library releases when consumers need automated changes between versions. A migration can update dependencies or modify project configuration and code to accommodate a breaking or structural change. Keep each migration focused on the transition it handles and make it suitable for running against a consumer’s project.

Author rules against a staged file tree

Schematic rules work with a virtual file tree rather than immediately editing the consumer’s files. Angular describes the virtual file system as a Tree with a base and a staging area: transformations are staged and applied after they are validated. This lets a rule compose file creation, template rendering, and other transformations before the changes reach the workspace. See Angular’s authoring guide.

For generated files, combine templates and utilities into rules that use the supplied options and relevant workspace or project data. Treat project selection as an explicit requirement in multi-project workspaces; a generator that silently targets the wrong project can be worse than one that asks the consumer to specify it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Build, package, and verify the consumer workflow

  1. Create the library’s schematics folder, collection manifest, and named schematic folders with their factories and option schemas.
  2. Reference the collection from the library package metadata and configure a separate build for schematic sources.
  3. Build the library and schematics, then confirm the compiled schematic output is included in the distributable package.
  4. Install or link that distributable into a representative Angular workspace and invoke the relevant CLI command, such as ng generate my-lib:my-service.
  5. Inspect the resulting files and configuration, and exercise the ng add or ng update path if the package provides those schematics.

Angular’s library guide illustrates building the library, packaging it, linking the distribution package, and invoking a generator from a workspace. The exact build commands and configuration depend on the Angular and DevKit versions in use, so use the guide as a workflow rather than copying version-sensitive configuration without checking it against the target project.

Keep the implementation aligned with the library

  • Document the names consumers can invoke and the options each command accepts.
  • Use ng-add only for installation-time work; put reusable artifact generation under a named schematic.
  • Add migrations when a release requires consumer-project changes, and keep them associated with the relevant version transition.
  • Test the packaged output in a workspace, not only the schematic source in isolation; the collection and compiled files must travel with the library for consumers to use them.

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.