Skip to main content
NgModules are TypeScript classes decorated with @NgModule that organize Angular applications into cohesive blocks of functionality. While standalone components are now the recommended approach, NgModules are still widely used in existing applications.
Standalone components are the recommended approach for new Angular applications. NgModules are considered legacy but remain fully supported.

What is an NgModule?

An NgModule is a container for components, directives, pipes, and services that work together as a functional unit:

NgModule Metadata

The @NgModule decorator accepts metadata:

declarations

Components, directives, and pipes that belong to this module:
Each component, directive, or pipe can only be declared in ONE NgModule. Declaring in multiple modules will cause a compilation error.

imports

Other modules whose exported classes are needed:

exports

Make components, directives, and pipes available to other modules:

providers

Services available to the module’s injector:

bootstrap

The root component(s) to bootstrap:

Common Module Patterns

Root Module (AppModule)

The main application module:

Core Module

Singleton services and app-wide components:

Shared Module

Reusable components, directives, and pipes:

Feature Module

Domain-specific functionality:

Lazy Loading Modules

Load modules on demand:

ModuleWithProviders

Configure module with runtime values:

Migration to Standalone

Migrate from NgModules to standalone components:
Use Angular CLI to help with migration:

Common Mistakes

Avoid these common NgModule mistakes:
  1. Declaring in multiple modules - Each component can only be in ONE NgModule
  2. Not importing CommonModule - Feature modules need CommonModule, not BrowserModule
  3. Importing modules in wrong order - RouterModule should typically be last
  4. Providing services in feature modules - Use providedIn: 'root' instead
  5. Re-importing HttpClientModule - Import only once in AppModule or CoreModule

Best Practices

  1. Consider standalone first - For new applications
  2. One core module - Import once in AppModule
  3. Shared module for common items - Import in feature modules
  4. Feature modules for domains - Organize by business domain
  5. Lazy load feature modules - Improve initial load time
  6. Use forRoot/forChild pattern - For configurable modules

Next Steps

Components

Learn about standalone components

Dependency Injection

Understand Angular’s DI system