Standalone Components
The default, not a feature
Section titled “The default, not a feature”Components, directives and pipes are standalone by default. There is no standalone: true to write — the flag exists only in its negative form, standalone: false, for declaring something in an NgModule.
@Component({ selector: 'order-list', imports: [OrderRow, DatePipe, RouterLink], template: `…`,})export class OrderList {}The Angular team’s position is explicit: use standalone components instead of NgModule for all new code. The NgModule documentation exists to help you understand existing code.
What changed and why
Section titled “What changed and why”Under NgModules, a component could not declare what it used. Its template’s dependencies came from the module that declared it, so understanding a component meant finding its module, then that module’s imports, then those modules’ exports. Adding one directive to one component meant editing a file the component did not reference.
The problems that produced:
- Indirection. A component’s dependencies were not visible from the component.
- Over-inclusion. A
SharedModuleexporting forty things gave every importer all forty, whether used or not. - Ceremony. Every new component meant a
declarationsentry somewhere else. - Weak tree-shaking. A bundler could not easily tell what was actually used.
Standalone components put the dependency list on the component:
imports: [OrderRow, DatePipe, RouterLink]You can see what the template can use, the compiler errors if you use something you did not import, and unused items are simply absent.
Bootstrapping
Section titled “Bootstrapping”bootstrapApplication(App, appConfig);
// app.config.tsexport const appConfig: ApplicationConfig = { providers: [ provideRouter(routes, withComponentInputBinding()), provideHttpClient(withInterceptors([authInterceptor])), provideClientHydration(), { provide: API_BASE_URL, useValue: 'https://api.example.test/v1' }, ],};No AppModule. Application-wide configuration is a providers array, assembled from provideX() functions that libraries export.
That pattern — provideRouter, provideHttpClient, provideClientHydration — is the standalone replacement for forRoot(). It is a genuine improvement: the functions are typed, their internals stay private, and optional capabilities (withInterceptors([...]), withXsrfConfiguration({...})) tree-shake away when not requested. If you ship anything reusable, ship a provideX() function rather than a providers array. See Dependency Injection in Angular.
Lazy loading
Section titled “Lazy loading”Standalone components can be lazy-loaded directly, without a module wrapper:
export const routes: Routes = [ { path: 'orders', loadComponent: () => import('./orders/order-list').then((m) => m.OrderList), }, { path: 'admin', canMatch: [isAdmin], providers: [AdminFeatureState], loadChildren: () => import('./admin/admin.routes').then((m) => m.adminRoutes), },];Route.providers creates a child EnvironmentInjector for the subtree — the standalone equivalent of a lazy module’s injector. It scopes which code can see the services; it does not, by default, destroy them when the user navigates away. See route injectors and their lifetime.
Standalone-only is also a prerequisite for @defer. Non-standalone dependencies inside a @defer block load eagerly regardless of the block, so an application still on NgModules cannot use template-level code splitting at all.
Directives and pipes
Section titled “Directives and pipes”The same applies:
@Directive({ selector: '[highlightOnHover]', /* ... */ })export class HighlightOnHover {}
@Pipe({ name: 'kebabCase' })export class KebabCasePipe implements PipeTransform {}Both are standalone by default and go in a consumer’s imports.
Migrating from NgModules
Section titled “Migrating from NgModules”ng generate @angular/core:standaloneThe schematic runs in three steps, and running them in order matters:
- Convert declarations to standalone — moves each component’s dependencies from its module into its own
imports. - Remove unnecessary NgModule classes — deletes modules that no longer do anything.
- Switch to standalone bootstrapping — replaces
bootstrapModulewithbootstrapApplication.
Practical notes from doing this on a real codebase:
- The two systems interoperate. A standalone component can be imported into an
NgModule, and anNgModulecan be imported by a standalone component. Migrate incrementally; there is no big-bang requirement. SharedModulebecomes a smell rather than an error. After conversion, every component that imported it lists only what it uses. Delete the module rather than converting it into a barrel of re-exports — you will have reintroduced the original problem with different syntax.- Watch
forRoot()providers. A module’sforRoot()providers need to move toApplicationConfig. The schematic handles the common cases; check any customforRootyourself. - Import lists get long, and that is fine. Twenty imports on one component is information, not noise — it usually means the component is doing too much, and the list is what tells you.
Where NgModules still appear
Section titled “Where NgModules still appear”Not everything is gone:
- Older libraries that still ship
NgModules. Import the module into a standalone component’simportsand move on. BrowserAnimationsModule-era APIs in codebases that have not adopted theprovide*equivalents.- AngularJS hybrids using
@angular/upgrade. - Large legacy applications mid-migration.
None of these are reasons to write a new NgModule.
Common mistakes
Section titled “Common mistakes”| Mistake | Consequence | Fix |
|---|---|---|
Writing standalone: true |
Redundant — it is the default | Omit it |
| Forgetting an import | “…is not a known element” or “…could not be found” | Add it to imports |
Recreating SharedModule as a barrel |
The original coupling, new syntax | Import what each component uses |
CommonModule imported for control flow |
Unnecessary — @if/@for need no import |
Remove it if nothing else from it is used |
| Application providers on a component | A per-instance provider, not a singleton | Put them in ApplicationConfig |
| Exporting a providers array from a library | Every token becomes public API | Export a provideX() function |
Expecting @defer to work with non-standalone deps |
Silently eager | Convert them first |
| Migrating by hand | Slow and error-prone | Use the schematic, then review |
Related pages
Section titled “Related pages”- Dependency Injection in Angular
- Angular Routing in Depth
- Control Flow:
@if,@for,@switchand@defer - Angular Performance
- Component basics
- Standalone and NgModule basics