Skip to content

Standalone Components

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.

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 SharedModule exporting forty things gave every importer all forty, whether used or not.
  • Ceremony. Every new component meant a declarations entry 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.

main.ts
bootstrapApplication(App, appConfig);
// app.config.ts
export 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.

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.

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.

Terminal window
ng generate @angular/core:standalone

The schematic runs in three steps, and running them in order matters:

  1. Convert declarations to standalone — moves each component’s dependencies from its module into its own imports.
  2. Remove unnecessary NgModule classes — deletes modules that no longer do anything.
  3. Switch to standalone bootstrapping — replaces bootstrapModule with bootstrapApplication.

Practical notes from doing this on a real codebase:

  • The two systems interoperate. A standalone component can be imported into an NgModule, and an NgModule can be imported by a standalone component. Migrate incrementally; there is no big-bang requirement.
  • SharedModule becomes 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’s forRoot() providers need to move to ApplicationConfig. The schematic handles the common cases; check any custom forRoot yourself.
  • 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.

Not everything is gone:

  • Older libraries that still ship NgModules. Import the module into a standalone component’s imports and move on.
  • BrowserAnimationsModule-era APIs in codebases that have not adopted the provide* equivalents.
  • AngularJS hybrids using @angular/upgrade.
  • Large legacy applications mid-migration.

None of these are reasons to write a new NgModule.

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