
You Don't Need NgModules: Standalone Components Are the Default
- كاتب موثّق
- Angular 22
محتويات المقال
PriceTag component to a fresh Angular 22 project and uses <app-price-tag> on the cart page. The build stops with NG8001: 'app-price-tag' is not a known element. Every answer they find says "declare it in AppModule", but the project has no app.module.ts. So they create a SharedModule, mark PriceTag with standalone: false, declare and export it, import the module into the cart, and the error goes away. A month later, SharedModule declares forty things and every page imports it. The real fix was one word in the cart's imports array.
The common misconception
declarations of an NgModule told the compiler which components, directives and pipes a template could use, and lazy loading meant a feature module. Years of tutorials, Stack Overflow answers and enterprise codebases are built on that model, and they're still out there. There's a true part too: NgModules aren't deprecated, many libraries still ship them, and the docs keep a full guide for them. What changed is that you no longer need them, for components or for structure.
What actually happens
A component is its own compilation context
imports array lists the components, directives, pipes and NgModules its template uses. Since Angular 19, standalone defaults to true, so you don't write it at all. Only older code carries standalone: false, the opt-out that lets a class be declared in a module, and a class can be declared in only one module.
Using a component means importing it
import { Component, NgModule, input } from '@angular/core';import { CommonModule } from '@angular/common';@Component({ selector: 'app-price-tag', standalone: false, template: `<span>{{ amount() | currency: 'EGP' }}</span>`,})export class PriceTag { readonly amount = input.required<number>();}@NgModule({ declarations: [PriceTag], imports: [CommonModule], exports: [PriceTag],})export class SharedModule {} // soon declares everything@Component({ selector: 'app-cart', imports: [SharedModule], // which parts does Cart use? Can't tell template: `<app-price-tag [amount]="200" />`,})export class Cart {}import { Component, input } from '@angular/core';import { CurrencyPipe } from '@angular/common';@Component({ selector: 'app-price-tag', imports: [CurrencyPipe], template: `<span>{{ amount() | currency: 'EGP' }}</span>`,})export class PriceTag { readonly amount = input.required<number>();}@Component({ selector: 'app-cart', imports: [PriceTag], // the template's dependencies, in one place template: `<app-price-tag [amount]="200" />`,})export class Cart {}Cart imports SharedModule and gets every component the module exports, so its file doesn't say what it really uses, and removing PriceTag one day means checking every template that might rely on it. In the standalone version, each dependency is a TypeScript import your editor can follow, and an element the component didn't import fails the build with NG8001 instead of rendering as an empty tag. The compiler helps in the other direction too: the unusedStandaloneImports diagnostic (NG8113) flags entries in imports that the template never uses.
The app boots from a component, not a module
import { bootstrapApplication } from '@angular/platform-browser';import { provideHttpClient } from '@angular/common/http';import { provideRouter, Routes } from '@angular/router';const routes: Routes = [ { path: '', component: Home }, { path: 'admin', children: adminRoutes, // a plain Routes array from the admin folder providers: [AdminApi], // injectable only inside the admin routes },];bootstrapApplication(App, { providers: [provideRouter(routes), provideHttpClient()],});bootstrapApplication renders a standalone root component and takes the app's providers as a plain array. Features that used to arrive as RouterModule.forRoot() or HttpClientModule now come as functions like provideRouter() and provideHttpClient(). A library that still ships only an NgModule keeps working: put the module in a component's imports to use its components, or wrap it in importProvidersFrom() to register its providers.
Structure comes from folders and routes
admin.routes.ts file that exports a Routes array. no longer needs a feature module: loadComponent loads a single standalone component, and if admin.routes.ts default-exports its array, loadChildren: () => import('./admin/admin.routes') turns the whole folder into a separate chunk. Services that belong to one feature go in that route's providers, so only the admin pages can inject them. Reuse works the way it always did: a ui/ folder of small standalone components gives you everything a SharedModule offered, except that each page imports only the two or three pieces it actually renders.
standalone: false classes go in declarations | |
SharedModule keeps imports tidy | |
AppModule to start | bootstrapApplication takes a root component and a providers array |
loadComponent and loadChildren load components and route arrays | |
imports, or its providers with importProvidersFrom() |
How to apply it in your project
Stop creating modules Generate new components, directives and pipes without standalone: false, and import each dependency in the component whose template uses it.Migrate in three passes Start from a clean branch, run the standalone schematic one mode at a time, and build and test after each pass. Expect some manual cleanup, especially in unit tests. Dissolve the SharedModule Replace each SharedModuleimport with the few pieces that component's template actually uses, then delete the module. NG8113 points out any imports left over.Draw boundaries with routes Give each feature a folder and a routes file, lazy-load it with loadChildren, and put feature-only services in the route'sproviders.
Key takeaways
Since Angular 19, components, directives and pipes are standalone by default; NgModules are optional. A component's importsarray is its compilation context: list exactly what its template uses.bootstrapApplicationwithprovide*()functions replaces the root module;importProvidersFrom()bridges old modules.Folders, route files, loadChildrenand routeprovidersgive you feature boundaries without a single NgModule.
التعليقات (0)