Skip to main content
Angular Basics
CHAPTER 16 Beginner

Angular Modules

Updated: May 18, 2026
5 min read

# CHAPTER 16

Angular Modules

1. Chapter Introduction

An Angular Module (NgModule) is a container that groups related components, directives, pipes, and services together. Every Angular app has at least one module — AppModule. As applications grow, you organize code into Feature Modules (e.g., AdminModule, AuthModule), Shared Modules (for reusable UI), and a Core Module (for singletons). Note: Modern Angular (v17+) increasingly uses Standalone Components that don't require modules, but understanding NgModules is still essential for most existing codebases.

2. Learning Objectives

  • Understand the @NgModule decorator and its arrays.
  • Create feature modules to organize your application.
  • Build a shared module for reusable components.
  • Configure lazy-loaded feature modules.

3. The @NgModule Decorator

app.module.ts
12345678910111213141516171819
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent    // Components, Directives, Pipes that BELONG to this module
  ],
  imports: [
    BrowserModule,  // Other modules this module DEPENDS ON
    FormsModule,
    AppRoutingModule
  ],
  providers: [],   // Services (rarely needed here since services use providedIn:'root')
  bootstrap: [AppComponent] // The ROOT component to launch the app
})
export class AppModule {}

4. Feature Modules

A Feature Module groups all code related to one domain area:
bash
1234
ng g module products --routing
ng g component products/product-list
ng g component products/product-detail
ng g service products/product
products.module.ts
1234567891011
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ProductsRoutingModule } from './products-routing.module';
import { ProductListComponent } from './product-list/product-list.component';
import { ProductDetailComponent } from './product-detail/product-detail.component';

@NgModule({
  declarations: [ProductListComponent, ProductDetailComponent],
  imports: [CommonModule, ProductsRoutingModule]
})
export class ProductsModule {}
text
123456789101112131415
App Structure with Feature Modules:

src/app/
├── auth/
│   ├── auth.module.ts
│   ├── login/
│   └── register/
├── products/
│   ├── products.module.ts
│   ├── product-list/
│   └── product-detail/
├── admin/
│   ├── admin.module.ts
│   └── dashboard/
└── app.module.ts

5. Shared Module

A Shared Module exports commonly used components and Angular modules to avoid importing them in every feature module:
typescript
1234567891011121314151617
// shared/shared.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ButtonComponent } from './button/button.component';
import { LoadingSpinnerComponent } from './loading-spinner/loading-spinner.component';

@NgModule({
  declarations: [ButtonComponent, LoadingSpinnerComponent],
  imports: [CommonModule, FormsModule, ReactiveFormsModule],
  exports: [
    // Export everything other modules will need
    CommonModule, FormsModule, ReactiveFormsModule,
    ButtonComponent, LoadingSpinnerComponent
  ]
})
export class SharedModule {}

Now any Feature Module just imports SharedModule:

typescript
1
imports: [SharedModule]  // Gets all shared components and modules

6. Core Module (For Singletons)

The Core Module is imported ONCE in AppModule and contains singleton services and app-wide components like Navbar and Footer:
typescript
1234567
// core/core.module.ts
@NgModule({
  declarations: [NavbarComponent, FooterComponent],
  imports: [CommonModule, RouterModule],
  exports: [NavbarComponent, FooterComponent]
})
export class CoreModule {}

7. Lazy Loading Feature Modules

app-routing.module.ts
1234567891011
const routes: Routes = [
  { path: '', component: HomeComponent },
  {
    path: 'products',
    loadChildren: () => import('./products/products.module').then(m => m.ProductsModule)
  },
  {
    path: 'admin',
    loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule)
  }
];

8. Common Mistakes

  • Declaring a component in two modules: A component can only belong to ONE module's declarations. If you need it in multiple places, move it to a SharedModule and export it.
  • Forgetting to export: If you add a component to a module's declarations but don't export it, other modules that import this module cannot use it.

9. MCQs with Answers

Question 1

What does the declarations array in @NgModule contain?

Question 2

What does the imports array in @NgModule contain?

Question 3

What does the exports array in @NgModule contain?

Question 4

What type of module groups reusable components and Angular modules shared across the app?

Question 5

What is the purpose of the Core Module?

Q6. Can a component be declared in two different NgModules simultaneously? a) Yes b) No, each component belongs to exactly one NgModule Answer: b) No, each belongs to exactly one NgModule.
Question 7

In modern Angular (v17+), what replaces the need for NgModules in many cases?

Question 8

What is lazy loading in terms of modules?

Question 9

What is the bootstrap array in AppModule used for?

Question 10

Why must CommonModule (not BrowserModule) be imported in Feature Modules?

10. Interview Questions

  • Q: Explain the difference between declarations, imports, and exports in an NgModule.
  • Q: What is the difference between BrowserModule and CommonModule?

11. Summary

NgModules are the organizational containers of Angular applications. By separating code into Feature Modules, Shared Modules, and a Core Module, large applications remain manageable. Lazy loading of Feature Modules significantly improves startup performance by deferring code until it is actually needed.

12. Next Chapter Recommendation

Components need to talk to each other! In Chapter 17: Component Communication, we will master @Input, @Output, EventEmitter, and service-based communication for parent-child and sibling component interactions.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·