Skip to main content

What are Schematics?

Schematics are template-based code generators that support complex logic. They are the engine behind ng generate commands and ng add package installations. Schematics can create, modify, delete, and move files while applying rules and templates to scaffold or modify code.
Think of schematics as “code recipes” - they describe how to generate or transform code in a repeatable, configurable way.

How Schematics Work

Schematics operate on a virtual file system called a tree. Changes are staged in memory before being committed to disk, allowing for validation and rollback.

Tree

Virtual file system representing project structure

Rule

Transformation applied to the tree

Template

File template with placeholders

Action

Create, update, delete, or rename operations

Official Schematics

Angular provides built-in schematics for common tasks:

Code Generation Schematics

Generates:
  • Component TypeScript file
  • Component template (HTML)
  • Component styles (CSS/SCSS)
  • Component test file
Options:
  • --standalone: Create standalone component
  • --inline-template: Use inline template
  • --inline-style: Use inline styles
  • --skip-tests: Skip test file
  • --flat: Don’t create folder
  • --export: Export from module

Migration Schematics

Angular provides powerful migration schematics:
Convert components, directives, and pipes to standalone:
Three-phase migration:
1

Convert to Standalone

Convert declarations to standalone and update imports
2

Remove NgModules

Delete unnecessary NgModule classes
3

Update Bootstrap

Switch to bootstrapApplication API
Options:
  • mode: Migration mode (convert, prune, bootstrap)
  • path: Relative path to migrate
See the standalone migration README for details.
Migrate to new control flow syntax:
Transforms:
  • *ngIf@if
  • *ngFor@for
  • *ngSwitch@switch
Before:
After:
Migrate to signal-based APIs:Signal Inputs:
Signal Queries:
Output Migration:
NgStyle to Style:
NgClass to Class:
Inject Migration:
Router Testing Module:
Cleanup Unused Imports:
All migration schematics support a --path option to migrate specific directories.

Creating Custom Schematics

Build your own code generators for common patterns in your application.

Setup

1

Install Schematics CLI

2

Create Schematics Collection

3

Project Structure

Define Schema

schema.json:
schema.d.ts:

Implement Schematic

index.ts:

Create Templates

files/__name@dasherize__/__name@dasherize__.component.ts:
files/__name@dasherize__/__name@dasherize__.component.html:

Template Functions

Angular provides utility functions for string transformation:

Schematic Rules

Rules define transformations applied to the tree:

Basic Rules

Chaining Rules

Conditional Rules


Advanced Features

Working with AST

Modify TypeScript files using Abstract Syntax Trees:

External Schematics

Call other schematics from your schematic:

Task Scheduling

Schedule tasks to run after schematic completes:

Testing Schematics

Test schematics using the testing utilities:

Publishing Schematics

1

Build the Package

2

Test Locally

3

Publish to npm

4

Use in Projects


Collection Configuration

collection.json:
string
required
Path to schematic implementation and export name
string
Path to JSON schema file defining options
string[]
Alternative names for the schematic
boolean
Hide from ng generate list

Best Practices

Always validate options and check for existing files:
Provide helpful feedback:
Test with --dry-run flag:
Shows what would be generated without creating files.
Use normalize and join for cross-platform compatibility:

Next Steps

CLI Overview

Return to CLI overview

Builders

Learn about custom builders

Resources

Schematics Guide

Official schematics documentation

Angular Schematics

Official Angular schematics source

DevKit Schematics

Schematics DevKit source

Example Schematics

Angular Core migration schematics