A shared module is a module that exports providers, components, or services so other modules can use them without redefining. It helps reuse code across the app.
Click to reveal answer
beginner
How do you make a module shared in NestJS?
You export the providers or components inside the module's @Module decorator using the 'exports' array. Then import this module in other modules to use the shared parts.
Click to reveal answer
intermediate
Does importing a shared module multiple times create multiple instances of its providers in NestJS?
No, NestJS automatically shares a single instance of providers across all modules that import it.
Click to reveal answer
beginner
What is the difference between 'imports' and 'exports' in a NestJS module?
'imports' bring in other modules to use their exported providers. 'exports' make providers from the current module available to other modules that import it.
Click to reveal answer
intermediate
Can a shared module import other modules? What happens then?
Yes, a shared module can import other modules. Those imported modules' exported providers become available inside the shared module, but are not automatically exported to modules importing the shared module.
Click to reveal answer
What must you do to make a service available to other modules in NestJS?
AAdd the service to the module's exports array
BAdd the service to the module's imports array
CDeclare the service in the main.ts file
DUse the service without importing the module
✗ Incorrect
You export the service in the module's exports array so other modules importing this module can use it.
If you import a shared module in multiple modules, what happens to its providers?
AThey are duplicated for each import
BThey are removed from the app
CThey are shared as single instances
DThey cause an error
✗ Incorrect
Providers in a shared module are singletons and shared across imports unless explicitly scoped differently.
Which decorator property lists modules that this module depends on?
Aexports
Bimports
Cproviders
Dcontrollers
✗ Incorrect
'imports' lists modules whose exported providers are needed inside this module.
What is the main benefit of using shared modules?
ATo reduce code duplication and reuse services
BTo increase app size
CTo create multiple instances of services
DTo avoid using providers
✗ Incorrect
Shared modules help reuse code and services across different parts of the app, reducing duplication.
If a shared module imports another module, do the imported module's exports become available to modules importing the shared module?
AYes, automatically
BNo, only inside the shared module
COnly if the imported module is global
DOnly if the shared module re-exports them
✗ Incorrect
The shared module must re-export the imported module's exports to make them available to modules importing the shared module.
Explain how to create and use a shared module in NestJS.
Think about how to share services across modules without rewriting them.
You got /4 concepts.
Describe the difference between 'imports' and 'exports' in a NestJS module and why both are important.
Consider how modules connect and share code.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a Shared Module in NestJS?
easy
A. To define database entities
B. To create a new controller for handling requests
C. To share providers and services across multiple modules
D. To configure middleware for the application
Solution
Step 1: Understand the role of Shared Modules
Shared modules are designed to hold providers and services that can be reused in different parts of the app.
Step 2: Compare with other module roles
Controllers handle requests, entities define data models, and middleware configures request handling, which are not the main purpose of shared modules.
Final Answer:
To share providers and services across multiple modules -> Option C
Quick Check:
Shared modules = share providers [OK]
Hint: Shared modules share providers, not controllers or entities [OK]
Common Mistakes:
Confusing shared modules with controllers
Thinking shared modules define database entities
Assuming shared modules configure middleware
2. Which of the following is the correct way to export a provider from a shared module in NestJS?
easy
A. Add the provider to the exports array in the module decorator
B. Add the provider to the controllers array
C. Add the provider to the imports array
D. Add the provider to the bootstrap array
Solution
Step 1: Recall module decorator properties
Providers are shared by listing them in the exports array of the module decorator.
Step 2: Understand other arrays
controllers is for controllers, imports is for importing other modules, and bootstrap is not used in modules.
Final Answer:
Add the provider to the exports array in the module decorator -> Option A
Quick Check:
Export providers via exports array [OK]
Hint: Use exports array to share providers [OK]
Common Mistakes:
Putting providers in controllers array
Confusing imports with exports
Using bootstrap array which doesn't exist in modules
3. Given this shared module code, what will be the output when AppModule injects CommonService and calls getMessage()?
import { Module } from '@nestjs/common';
import { CommonService } from './common.service';
@Module({
providers: [CommonService],
exports: [CommonService],
})
export class SharedModule {}
// CommonService
export class CommonService {
getMessage() {
return 'Hello from SharedModule';
}
}
// AppModule
import { Module } from '@nestjs/common';
import { SharedModule } from './shared.module';
@Module({
imports: [SharedModule],
})
export class AppModule {
constructor(private commonService: CommonService) {}
printMessage() {
console.log(this.commonService.getMessage());
}
}
medium
A. Error: CommonService not found
B. Undefined
C. Hello from SharedModule
D. Hello from AppModule
Solution
Step 1: Check provider export and import
SharedModule exports CommonService, and AppModule imports SharedModule, so CommonService is available in AppModule.
Step 2: Understand service method call
Calling getMessage() returns the string 'Hello from SharedModule'.
Final Answer:
Error: CommonService not found -> Option A
Quick Check:
Service must be injected via constructor and registered as provider [OK]
Hint: Import shared module to access exported services [OK]
Common Mistakes:
Forgetting to export the provider
Not importing the shared module in AppModule
Assuming service returns undefined
4. Identify the error in this shared module setup:
import { Module } from '@nestjs/common';
import { LoggerService } from './logger.service';
@Module({
providers: [LoggerService],
imports: [LoggerService],
exports: [LoggerService],
})
export class LoggerModule {}
medium
A. LoggerService must be declared as a global provider
B. LoggerService should not be in the imports array
C. LoggerService should not be exported
D. LoggerService must be in controllers array
Solution
Step 1: Review module decorator arrays
The imports array is for importing other modules, not providers or services.
Step 2: Identify correct placement
LoggerService should be in providers and exports, but not in imports.
Final Answer:
LoggerService should not be in the imports array -> Option B
Quick Check:
Imports array = modules only [OK]
Hint: Imports array only accepts modules, not providers [OK]
Common Mistakes:
Putting providers in imports array
Confusing controllers with providers
Thinking exports are optional for sharing
5. You have two modules, FeatureModule and SharedModule. SharedModule exports UtilityService. You want FeatureModule to use UtilityService and also export it for other modules. How should you configure FeatureModule to achieve this?
hard
A. Import SharedModule and add UtilityService to FeatureModule controllers
B. Only import SharedModule in FeatureModule, no exports needed
C. Declare UtilityService again in FeatureModule providers and exports
D. Import SharedModule and export UtilityService in FeatureModule's exports array
Solution
Step 1: Import SharedModule in FeatureModule
To use UtilityService, FeatureModule must import SharedModule which exports it.
Step 2: Export UtilityService from FeatureModule
To share UtilityService further, FeatureModule must export it in its exports array.
Final Answer:
Import SharedModule and export UtilityService in FeatureModule's exports array -> Option D
Quick Check:
Import then export to re-share service [OK]
Hint: Import then export shared service to re-share it [OK]