0
0
NestJSframework~20 mins

Shared modules in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Shared Modules Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does a Shared Module affect service instances in NestJS?

Consider a NestJS Shared Module that exports a service. What happens to the service instance when this Shared Module is imported into multiple feature modules?

NestJS
import { Module } from '@nestjs/common';
import { SharedService } from './shared.service';

@Module({
  providers: [SharedService],
  exports: [SharedService],
})
export class SharedModule {}

// FeatureModuleA and FeatureModuleB both import SharedModule
ASharedService is not instantiated unless explicitly provided in each feature module.
BEach feature module gets its own separate instance of SharedService.
CSharedService instances are created per request in each feature module.
DAll feature modules share the same single instance of SharedService.
Attempts:
2 left
💡 Hint

Think about how NestJS handles providers exported from a shared module.

📝 Syntax
intermediate
2:00remaining
Correct way to define a Shared Module in NestJS

Which of the following code snippets correctly defines a Shared Module that exports a service for reuse?

A
import { Module } from '@nestjs/common';
import { SharedService } from './shared.service';

@Module({
  providers: [SharedService],
  exports: [SharedService],
})
export class SharedModule {}
B
import { Module } from '@nestjs/common';
import { SharedService } from './shared.service';

@Module({
  imports: [SharedService],
  exports: [SharedService],
})
export class SharedModule {}
C
import { Module } from '@nestjs/common';
import { SharedService } from './shared.service';

@Module({
  providers: [SharedService],
  imports: [SharedService],
})
export class SharedModule {}
D
import { Module } from '@nestjs/common';
import { SharedService } from './shared.service';

@Module({
  exports: [SharedService],
})
export class SharedModule {}
Attempts:
2 left
💡 Hint

Remember that services must be listed as providers to be injectable.

🔧 Debug
advanced
2:00remaining
Why does importing a Shared Module twice cause multiple instances?

Given a Shared Module imported into two different modules, why might you see multiple instances of a service instead of one?

NestJS
import { Module } from '@nestjs/common';
import { SharedService } from './shared.service';

@Module({
  providers: [SharedService],
  exports: [SharedService],
})
export class SharedModule {}

// AppModule imports SharedModule twice accidentally
ABecause SharedService is not exported, so each module creates its own instance.
BBecause the Shared Module is imported multiple times without being global, NestJS creates separate instances per import.
CBecause SharedService is marked as transient, so new instances are created each time.
DBecause the Shared Module is lazy loaded, causing multiple instances.
Attempts:
2 left
💡 Hint

Consider how NestJS handles module imports and singleton scope.

state_output
advanced
2:00remaining
What is the output when a Shared Service maintains state across modules?

Consider a SharedService with a counter property incremented by two different feature modules. What will be the final counter value after both modules increment it once?

NestJS
import { Injectable } from '@nestjs/common';

@Injectable()
export class SharedService {
  counter = 0;
  increment() {
    this.counter++;
  }
}

// Both FeatureModuleA and FeatureModuleB import SharedModule which exports SharedService
// Each calls sharedService.increment() once
A2
B1
C0
DError: counter is undefined
Attempts:
2 left
💡 Hint

Think about whether the service instance is shared or duplicated.

🧠 Conceptual
expert
2:00remaining
How to make a Shared Module globally available in NestJS?

Which approach correctly makes a Shared Module globally available so it does not need to be imported in every module?

ADeclare the Shared Module inside main.ts bootstrap function.
BUse the 'global: true' option inside @Module decorator.
CAdd @Global() decorator to the Shared Module and export the providers.
DImport the Shared Module in the root AppModule only.
Attempts:
2 left
💡 Hint

Check NestJS documentation about global modules.