@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.
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:Common Mistakes
Best Practices
- Consider standalone first - For new applications
- One core module - Import once in AppModule
- Shared module for common items - Import in feature modules
- Feature modules for domains - Organize by business domain
- Lazy load feature modules - Improve initial load time
- Use forRoot/forChild pattern - For configurable modules
Next Steps
Components
Learn about standalone components
Dependency Injection
Understand Angular’s DI system
