0
0
NestJSframework~20 mins

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

Choose your learning style9 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.