FresherTechnicalAngular
What is an NgModule and why do new Angular apps skip it?
Try answering first
Model answer
An NgModule is a class decorated with @NgModule that groups components, directives and pipes (declarations), what they need (imports) and what they share (exports). Before standalone components, every component had to belong to exactly one module.
@NgModule({
declarations: [UserCard],
imports: [CommonModule],
exports: [UserCard],
})
export class UserModule {}
New apps skip modules because standalone components list their own dependencies in imports, which is simpler:
- Less boilerplate and no "which module declares this?" question.
- Better tree-shaking and lazy loading per component.
- Easier to read, since a component shows everything it uses.
Since Angular 19, components are standalone by default. To declare one in an NgModule you must write standalone: false. You will still meet NgModules in older code and in some libraries, and the two styles work together.
Write standalone components and learn NgModules for reading legacy code.
Key points
- NgModule groups declarations, imports and exports
- Standalone components declare their own imports
- Components are standalone by default since v19
- Both styles can coexist during migration
Likely follow-ups
- How do you use an NgModule-based library from a standalone component?
Common mistakes
- Declaring a standalone component in an NgModule declarations array
- Believing NgModules were removed from Angular