Want to keep your NestJS app neat and easy to grow? Feature modules are the key!
Why Feature modules in NestJS? - Purpose & Use Cases
Imagine building a large NestJS app where all your controllers, services, and providers are in one big file or folder.
Every time you add a new feature, you have to scroll through tons of code to find where to put it.
Managing everything in one place gets confusing fast.
It's easy to accidentally break something when changing unrelated parts.
Testing and reusing code becomes a nightmare.
Feature modules let you group related code together.
Each feature lives in its own module with its own controllers and services.
This keeps your app organized, easier to maintain, and scalable.
app.controller.ts, app.service.ts, all in one folderusers.module.ts with users.controller.ts and users.service.ts inside
It enables clean separation of concerns and easy scaling of your NestJS app.
Think of an online store app: you can have separate feature modules for products, orders, and users, each managing its own logic.
Feature modules organize code by functionality.
They make large apps easier to manage and scale.
They improve code reuse and testing.
Practice
Solution
Step 1: Understand the role of feature modules
Feature modules group related parts of the app like controllers and services to keep code organized.Step 2: Compare options with this role
Only To group related controllers and services for better organization describes grouping related controllers and services, which matches the purpose.Final Answer:
To group related controllers and services for better organization -> Option BQuick Check:
Feature modules organize code = D [OK]
- Thinking feature modules handle database connections directly
- Confusing feature modules with the main app module
- Assuming feature modules replace controllers
Solution
Step 1: Recall the decorator for modules
Feature modules use the @Module decorator with metadata like controllers and providers.Step 2: Match options with correct decorator
Only @Module({ controllers: [], providers: [] }) export class FeatureModule {} uses @Module with controllers and providers arrays, which is correct syntax.Final Answer:
@Module({ controllers: [], providers: [] }) export class FeatureModule {} -> Option CQuick Check:
@Module decorator defines modules = B [OK]
- Using @Controller or @Injectable instead of @Module
- Forgetting to add the decorator
- Defining empty classes without decorators
getHello() method is called from HelloController?
@Module({
controllers: [HelloController],
providers: [HelloService],
})
export class HelloModule {}
@Injectable()
export class HelloService {
getHello() {
return 'Hello from service!';
}
}
@Controller()
export class HelloController {
constructor(private readonly helloService: HelloService) {}
@Get()
getHello() {
return this.helloService.getHello();
}
}Solution
Step 1: Understand dependency injection in the module
The HelloService is provided in the module and injected into HelloController.Step 2: Trace the method call
The controller's getHello calls helloService.getHello(), which returns 'Hello from service!'.Final Answer:
"Hello from service!" -> Option DQuick Check:
Service method returns string = C [OK]
- Assuming controller returns its own string
- Thinking provider is missing causing error
- Confusing method names or return values
@Module({
controllers: [UserController],
providers: [UserService],
})
export class UserModule {}
@Injectable()
export class UserService {}
@Controller()
export class UserController {
constructor(userService: UserService) {}
}Solution
Step 1: Check constructor injection syntax
In NestJS, injected dependencies must have an access modifier like private or public to create a class property.Step 2: Identify the missing modifier
The constructor parameter 'userService' lacks 'private' or 'public', so it won't be assigned automatically.Final Answer:
Missing access modifier 'private' in constructor injection -> Option AQuick Check:
Constructor injection needs access modifier = A [OK]
- Forgetting private/public in constructor parameters
- Assuming @Injectable is missing when it is present
- Confusing decorator placement
AuthModule into UserModule?Solution
Step 1: Understand module imports in NestJS
To reuse services from another module, you must import that module in the imports array.Step 2: Identify correct import usage
@Module({ imports: [AuthModule], controllers: [UserController], providers: [UserService] }) export class UserModule {} correctly adds AuthModule to imports, allowing UserModule to use its exported providers.Final Answer:
@Module({ imports: [AuthModule], controllers: [UserController], providers: [UserService] }) export class UserModule {} -> Option AQuick Check:
Import modules via imports array = A [OK]
- Trying to inject modules via constructor instead of imports
- Using exports instead of imports to include modules
- Leaving imports array empty when reusing modules
Ready to prove it?
Pass the test and this pattern moves unknown
