Bird
Raised Fist0
NestJSframework~20 mins

Shared modules in NestJS - Practice Problems & Coding Challenges

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

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

  1. 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.
  2. 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.
  3. Final Answer:

    To share providers and services across multiple modules -> Option C
  4. 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

  1. Step 1: Recall module decorator properties

    Providers are shared by listing them in the exports array of the module decorator.
  2. Step 2: Understand other arrays

    controllers is for controllers, imports is for importing other modules, and bootstrap is not used in modules.
  3. Final Answer:

    Add the provider to the exports array in the module decorator -> Option A
  4. 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

  1. Step 1: Check provider export and import

    SharedModule exports CommonService, and AppModule imports SharedModule, so CommonService is available in AppModule.
  2. Step 2: Understand service method call

    Calling getMessage() returns the string 'Hello from SharedModule'.
  3. Final Answer:

    Error: CommonService not found -> Option A
  4. 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

  1. Step 1: Review module decorator arrays

    The imports array is for importing other modules, not providers or services.
  2. Step 2: Identify correct placement

    LoggerService should be in providers and exports, but not in imports.
  3. Final Answer:

    LoggerService should not be in the imports array -> Option B
  4. 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

  1. Step 1: Import SharedModule in FeatureModule

    To use UtilityService, FeatureModule must import SharedModule which exports it.
  2. Step 2: Export UtilityService from FeatureModule

    To share UtilityService further, FeatureModule must export it in its exports array.
  3. Final Answer:

    Import SharedModule and export UtilityService in FeatureModule's exports array -> Option D
  4. Quick Check:

    Import then export to re-share service [OK]
Hint: Import then export shared service to re-share it [OK]
Common Mistakes:
  • Not exporting UtilityService from FeatureModule
  • Re-declaring UtilityService providers unnecessarily
  • Adding services to controllers array
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