What is the purpose of the @Module decorator in NestJS?
The @Module decorator defines a module in NestJS. It organizes related components like controllers, providers, and imports into a single unit, helping structure the app clearly.
Click to reveal answer
beginner
Name the main metadata properties used inside the @Module decorator.
The main properties are: • imports: other modules to include • controllers: classes handling incoming requests • providers: services or classes that provide functionality • exports: providers made available to other modules
Click to reveal answer
intermediate
How does the 'imports' property in @Module metadata work?
The 'imports' property lists other modules whose exported providers are needed in this module. It allows sharing functionality between modules.
Click to reveal answer
intermediate
What happens if you forget to add a provider to the 'providers' array in a module?
NestJS will not recognize the provider as injectable in that module, causing errors when trying to use it in controllers or other providers.
Click to reveal answer
intermediate
Explain the role of the 'exports' property in the @Module decorator.
The 'exports' property makes selected providers available to other modules that import this module. It helps share services across different parts of the app.
Click to reveal answer
Which property in the @Module decorator lists the controllers of the module?
Acontrollers
Bproviders
Cimports
Dexports
✗ Incorrect
The 'controllers' property holds the classes that handle incoming requests in the module.
What does the 'providers' array in a module contain?
AOther modules to import
BControllers for handling requests
CComponents for UI rendering
DServices and classes that provide functionality
✗ Incorrect
The 'providers' array contains services or classes that provide business logic or functionality.
If you want to use a service from another module, which property should you use in your module?
Acontrollers
Bproviders
Cimports
Dexports
✗ Incorrect
You add the other module to the 'imports' array to use its exported providers.
What is the effect of adding a provider to the 'exports' array?
AIt makes the provider available to modules that import this module
BIt hides the provider from other modules
CIt registers the provider as a controller
DIt deletes the provider
✗ Incorrect
Adding a provider to 'exports' shares it with other modules that import this module.
Which decorator is used to define a module in NestJS?
A@Injectable
B@Module
C@Component
D@Controller
✗ Incorrect
The '@Module' decorator defines a module and its metadata.
Describe how the @Module decorator organizes an application in NestJS.
Think about how a folder groups related files.
You got /3 concepts.
Explain the roles of 'imports' and 'exports' in the @Module metadata.
Consider how you borrow and lend things between friends.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the @Module decorator in NestJS?
easy
A. To define the database connection settings
B. To group related components like controllers and providers together
C. To create HTTP routes directly
D. To style the application UI
Solution
Step 1: Understand the role of @Module
The @Module decorator organizes related parts of the app such as controllers and providers.
Step 2: Compare options
The options describing database connections, HTTP routing, or UI styling are unrelated to the main role of @Module.
Final Answer:
To group related components like controllers and providers together -> Option B
Quick Check:
@Module groups controllers & providers [OK]
Hint: Remember: @Module bundles controllers and providers [OK]
Common Mistakes:
Thinking @Module sets up database connections
Confusing @Module with routing decorators
Assuming @Module styles the UI
2. Which of the following is the correct way to define a NestJS module with a controller named AppController and a provider named AppService?
easy
A. @Module({ controller: [AppController], provider: [AppService] })
B. @Module({ controllers: AppController, providers: AppService })
C. @Module({ controllers: (AppController), providers: (AppService) })
D. @Module({ controllers: [AppController], providers: [AppService] })
Solution
Step 1: Check correct property names and syntax
The @Module decorator expects arrays for controllers and providers properties.
Step 2: Validate each option
Singular property names like 'controller'/'provider', missing array brackets, or parentheses instead of square brackets are incorrect. The correct syntax uses plural 'controllers'/'providers' with arrays.
Final Answer:
@Module({ controllers: [AppController], providers: [AppService] }) -> Option D
Quick Check:
controllers & providers must be arrays [OK]
Hint: Use arrays for controllers and providers in @Module [OK]
Common Mistakes:
Using singular property names like controller or provider
Not wrapping controllers/providers in arrays
Using parentheses instead of square brackets
3. Given the following module definition, what will be the value of AppModule.imports.length?
A. imports should be an array, not a single module
B. controllers should not be inside an array
C. providers property is missing
D. Module class must have a constructor
Solution
Step 1: Check the imports property type
The imports property must be an array, but here it is a single module without brackets.
Step 2: Verify other properties
controllers and providers are correctly arrays. The module class does not require a constructor by default.
Final Answer:
imports should be an array, not a single module -> Option A
Quick Check:
imports must be array [OK]
Hint: Always wrap imports in square brackets [OK]
Common Mistakes:
Using single module without array in imports
Thinking controllers can't be arrays
Assuming constructor is mandatory
5. You want to share a service SharedService from SharedModule so other modules can use it. Which is the correct way to configure SharedModule to allow this?
hard
A. @Module({ imports: [SharedService], exports: [SharedService] })
B. @Module({ providers: [SharedService] })
C. @Module({ providers: [SharedService], exports: [SharedService] })
D. @Module({ exports: [SharedService] })
Solution
Step 1: Understand sharing services via exports
To share a service, it must be both provided and exported in the module metadata.
Step 2: Analyze options
Sharing requires listing the service in both providers (to provide it) and exports (to make it available to importers). imports is for modules, not services. Missing either providers or exports prevents sharing.
Final Answer:
@Module({ providers: [SharedService], exports: [SharedService] }) -> Option C
Quick Check:
providers AND exports needed [OK]
Hint: Provide and export service to share it with other modules [OK]
Common Mistakes:
Forgetting to export the service
Trying to import a service instead of providing it
Only exporting without providing the service
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.