الانتقال إلى المحتوى
FrontendAngular· 7 دقائق للقراءة

You Don't Need NgModules: Standalone Components Are the Default

A component imports what its template uses. Folders and routes do the organizing modules used to do.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • Angular 22
محتويات المقال

A developer adds a 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

The belief: every component has to be declared in an NgModule, and even if small demos skip them, real apps need modules to group features, share components and lazy-load pages.

For most of Angular's history, that was simply how Angular worked. The 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

The compiler still needs to know what every tag, attribute and pipe in a template refers to. A answers that itself: its 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

Here is the cart from the bug report, written both ways:

TypeScript shared.module.ts
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 {}

Both versions render the same thing. In the module version, 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

TypeScript main.ts
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

Modules were also how teams drew feature boundaries, and that job moved to things you already have. A feature is a folder with its components and an 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.

MythReality
Every component must be declared in an NgModuleStandalone is the default since v19; only standalone: false classes go in declarations
A SharedModule keeps imports tidyIt hides what each template uses; direct imports are explicit and checked
The app needs an AppModule to startbootstrapApplication takes a root component and a providers array
Lazy loading needs a feature moduleloadComponent and loadChildren load components and route arrays
Library NgModules can't be used anymoreAdd the module to imports, or its providers with importProvidersFrom()

How to apply it in your project

  1. Stop creating modulesGenerate new components, directives and pipes without standalone: false, and import each dependency in the component whose template uses it.
  2. Migrate in three passesStart 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.
  3. Dissolve the SharedModuleReplace each SharedModule import with the few pieces that component's template actually uses, then delete the module. NG8113 points out any imports left over.
  4. Draw boundaries with routesGive each feature a folder and a routes file, lazy-load it with loadChildren, and put feature-only services in the route's providers.

Key takeaways

  • Since Angular 19, components, directives and pipes are standalone by default; NgModules are optional.
  • A component's imports array is its compilation context: list exactly what its template uses.
  • bootstrapApplication with provide*() functions replaces the root module; importProvidersFrom() bridges old modules.
  • Folders, route files, loadChildren and route providers give you feature boundaries without a single NgModule.

📚 المصادر

  1. Anatomy of components — Angular
  2. NgModules — Angular
  3. Migrate an existing Angular project to standalone — Angular
  4. Route loading strategies — Angular
  5. bootstrapApplication — Angular API

التعليقات (0)