0
0
NestJSframework~20 mins

Module re-exporting in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Module Re-exporting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is the main purpose of module re-exporting in NestJS?

In NestJS, why would you use module re-exporting?

ATo automatically generate controllers for all imported modules
BTo duplicate providers across multiple modules for better performance
CTo prevent any module from accessing shared services
DTo allow other modules to access providers from a module without importing it directly
Attempts:
2 left
💡 Hint

Think about how to share services without importing every module everywhere.

component_behavior
intermediate
2:00remaining
What will be the output when accessing a service from a re-exported module?

Given the following NestJS modules, what will happen when AppModule injects SharedService?

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

@Injectable()
export class SharedService {
  getMessage() {
    return 'Hello from SharedService';
  }
}

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

@Module({
  imports: [SharedModule],
  exports: [SharedModule],
})
export class CoreModule {}

@Module({
  imports: [CoreModule],
})
export class AppModule {
  constructor(private sharedService: SharedService) {}
  getMessage() {
    return this.sharedService.getMessage();
  }
}
A'Hello from SharedService' will be returned successfully
BA runtime error because SharedService is not provided in AppModule
CA compile-time error due to missing provider in CoreModule
DAppModule will create a new instance of SharedService ignoring CoreModule
Attempts:
2 left
💡 Hint

Consider how exports and imports work in NestJS modules.

📝 Syntax
advanced
1:30remaining
Identify the correct way to re-export a module in NestJS

Which of the following code snippets correctly re-exports SharedModule inside CoreModule?

A
import { Module } from '@nestjs/common';
import { SharedModule } from './shared.module';

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

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

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

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

Remember that to re-export a module, it must be both imported and exported.

🔧 Debug
advanced
2:00remaining
Why does injecting a re-exported service cause a runtime error?

Consider these modules:

import { Module, Injectable } from '@nestjs/common';

@Injectable()
export class ServiceA {}

@Module({
  providers: [ServiceA],
})
export class ModuleA {}

@Module({
  imports: [ModuleA],
  exports: [ModuleA],
})
export class ModuleB {}

@Module({
  imports: [ModuleB],
})
export class AppModule {
  constructor(private serviceA: ServiceA) {}
}

Why does NestJS throw a runtime error that ServiceA is not available in AppModule?

ABecause ServiceA is not decorated with @Injectable()
BBecause ModuleB forgot to import ServiceA directly
CBecause ModuleA does not export ServiceA, so ModuleB cannot re-export it
DBecause AppModule must import ModuleA directly to access ServiceA
Attempts:
2 left
💡 Hint

Check what each module exports and imports.

state_output
expert
2:30remaining
What is the number of instances created for a re-exported provider?

Given these modules and service, how many instances of SingletonService will be created when AppModule injects it?

import { Module, Injectable } from '@nestjs/common';

@Injectable()
export class SingletonService {
  id = Math.random();
}

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

@Module({
  imports: [SharedModule],
  exports: [SharedModule],
})
export class CoreModule {}

@Module({
  imports: [CoreModule, SharedModule],
})
export class AppModule {
  constructor(public coreService: SingletonService, public sharedService: SingletonService) {}
}

How many different instances of SingletonService exist in AppModule?

AOne instance shared between coreService and sharedService
BTwo separate instances, one for each injection
CZero instances because of circular imports
DThree instances due to multiple imports
Attempts:
2 left
💡 Hint

Think about how NestJS handles singleton providers across modules.