Bird
Raised Fist0
NestJSframework~20 mins

Module decorator and metadata in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
NestJS Module Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What does this NestJS module output when imported?
Consider this NestJS module definition. What will be the value of exports metadata after compilation?
NestJS
import { Module } from '@nestjs/common';
import { ServiceA } from './serviceA';

@Module({
  providers: [ServiceA],
  exports: [ServiceA],
})
export class MyModule {}
A[ServiceA]
B[]
C[MyModule]
D[ServiceA, MyModule]
Attempts:
2 left
💡 Hint
Look at what is explicitly listed in the exports array inside the @Module decorator.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a NestJS module with imports and controllers?
Select the code snippet that correctly uses the @Module decorator with imports and controllers arrays.
A
import { Module } from '@nestjs/common';
@Module({
  imports: OtherModule,
  controllers: AppController,
})
export class AppModule {}
B
import { Module } from '@nestjs/common';
@Module({
  imports: [OtherModule],
  controllers: [AppController],
})
export class AppModule {}
C
import { Module } from '@nestjs/common';
@Module({
  imports: {OtherModule},
  controllers: {AppController},
})
export class AppModule {}
D
import { Module } from '@nestjs/common';
@Module({
  imports: 'OtherModule',
  controllers: 'AppController',
})
export class AppModule {}
Attempts:
2 left
💡 Hint
Remember that imports and controllers expect arrays, not single values or objects.
🔧 Debug
advanced
2:00remaining
Why does this NestJS module fail to provide ServiceB to other modules?
Given this module, why can't other modules inject ServiceB?
NestJS
import { Module } from '@nestjs/common';
import { ServiceB } from './serviceB';

@Module({
  providers: [ServiceB],
})
export class FeatureModule {}
AServiceB must be declared in controllers, not providers.
BServiceB is not listed in the imports array, so it can't be injected.
CThe module is missing the @Injectable decorator on ServiceB.
DServiceB is not exported in the module's exports array, so it's not available outside FeatureModule.
Attempts:
2 left
💡 Hint
Think about how NestJS shares providers between modules.
🧠 Conceptual
advanced
2:00remaining
What is the role of the 'imports' array in the @Module decorator?
Choose the best description of what the 'imports' array does in a NestJS module.
AIt declares the controllers that belong to this module.
BIt defines the components that should be bootstrapped at application start.
CIt lists other modules whose exported providers are required by this module.
DIt specifies the services that this module provides to others.
Attempts:
2 left
💡 Hint
Think about how modules share providers in NestJS.
❓ state_output
expert
2:00remaining
What is the value of 'exports' metadata after this module is compiled?
Analyze this NestJS module and determine the final 'exports' array value.
NestJS
import { Module } from '@nestjs/common';
import { ServiceX } from './serviceX';
import { ServiceY } from './serviceY';

@Module({
  providers: [ServiceX, ServiceY],
  exports: [ServiceX],
})
export class SharedModule {}
A[ServiceX]
B[ServiceY]
C[]
D[ServiceX, ServiceY]
Attempts:
2 left
💡 Hint
Only what is listed in exports is shared outside the module.

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

  1. Step 1: Understand the role of @Module

    The @Module decorator organizes related parts of the app such as controllers and providers.
  2. Step 2: Compare options

    The options describing database connections, HTTP routing, or UI styling are unrelated to the main role of @Module.
  3. Final Answer:

    To group related components like controllers and providers together -> Option B
  4. 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

  1. Step 1: Check correct property names and syntax

    The @Module decorator expects arrays for controllers and providers properties.
  2. 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.
  3. Final Answer:

    @Module({ controllers: [AppController], providers: [AppService] }) -> Option D
  4. 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?
@Module({
  imports: [CommonModule, DatabaseModule],
  controllers: [AppController],
  providers: [AppService]
})
export class AppModule {}
medium
A. 2
B. 1
C. 0
D. Undefined

Solution

  1. Step 1: Identify the imports array length

    The imports array contains two modules: CommonModule and DatabaseModule.
  2. Step 2: Calculate the length property

    Since there are two items, AppModule.imports.length equals 2.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    imports array length = 2 [OK]
Hint: Count modules inside imports array for length [OK]
Common Mistakes:
  • Assuming imports is empty or undefined
  • Confusing imports with controllers or providers
  • Forgetting to count all items in the array
4. Identify the error in this NestJS module definition:
@Module({
  imports: CommonModule,
  controllers: [AppController],
  providers: [AppService]
})
export class AppModule {}
medium
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

  1. Step 1: Check the imports property type

    The imports property must be an array, but here it is a single module without brackets.
  2. Step 2: Verify other properties

    controllers and providers are correctly arrays. The module class does not require a constructor by default.
  3. Final Answer:

    imports should be an array, not a single module -> Option A
  4. 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

  1. Step 1: Understand sharing services via exports

    To share a service, it must be both provided and exported in the module metadata.
  2. 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.
  3. Final Answer:

    @Module({ providers: [SharedService], exports: [SharedService] }) -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard