0
0
NestJSframework~20 mins

Root module (AppModule) in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Root Module Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when the AppModule imports a service but does not provide it?

Consider a NestJS AppModule that imports a service module but does not list the service in its providers array. What happens when a component inside AppModule tries to inject that service?

NestJS
import { Module } from '@nestjs/common';
import { ServiceModule } from './service.module';

@Module({
  imports: [ServiceModule],
  controllers: [],
  providers: [],
})
export class AppModule {}

// ServiceModule provides SomeService
// A controller in AppModule tries to inject SomeService
AThe injection works fine because the service is exported by ServiceModule and imported by AppModule.
BThe injection fails with a runtime error because AppModule does not provide the service directly.
CThe injection fails with a compile-time error due to missing provider in AppModule.
DThe service is instantiated twice, once in ServiceModule and once in AppModule.
Attempts:
2 left
💡 Hint

Think about how NestJS shares providers between modules when they are exported and imported.

📝 Syntax
intermediate
2:00remaining
Which AppModule definition is syntactically correct in NestJS?

Choose the correct syntax for defining a root AppModule in NestJS.

A
import { Module } from '@nestjs/common';

@Module({
  imports: [],
  controllers: [],
  providers: [],
})
export default class AppModule {}
B
import { Module } from '@nestjs/common';

@Module({
  imports: [],
  controllers: [],
  providers: [],
})
export class AppModule {}
C
import { Module } from '@nestjs/common';

@Module({
  imports: [],
  controllers: [],
  providers: [],
})
class AppModule {}
D
}{ eludoMppA ssalc tropxe
)}
,][ :sredivorp  
,][ :srellortnoc  
,][ :stropmi  
{(eludoM@

;'nommoc/sjtsen@' morf } eludoM { tropmi
Attempts:
2 left
💡 Hint

Remember to use the correct export syntax and include semicolons.

lifecycle
advanced
2:00remaining
What happens if AppModule imports two modules that provide the same service?

If AppModule imports ModuleA and ModuleB, and both provide the same service class, what will be the behavior when injecting that service in AppModule?

AThe service from the last imported module overrides the previous one.
BNestJS creates two separate instances and injection is ambiguous causing an error.
CNestJS throws a runtime error due to duplicate provider tokens.
DNestJS uses the first imported module's service and ignores the second.
Attempts:
2 left
💡 Hint

Think about how NestJS resolves providers when duplicates exist in imports.

🔧 Debug
advanced
2:00remaining
Why does this AppModule fail to start with 'Nest can't resolve dependencies' error?

Examine the following AppModule code and identify the cause of the dependency resolution error.

NestJS
import { Module } from '@nestjs/common';
import { ServiceA } from './serviceA';

@Module({
  imports: [],
  controllers: [],
  providers: [ServiceA],
})
export class AppModule {}

// ServiceA depends on ServiceB but ServiceB is not provided anywhere.
AAppModule must import ServiceA's module instead of providing ServiceA directly.
BServiceA is not exported, causing the error.
CServiceB is missing from providers, so ServiceA's dependency cannot be resolved.
DThe error is caused by missing controllers array.
Attempts:
2 left
💡 Hint

Check the dependencies of ServiceA and whether they are provided.

🧠 Conceptual
expert
2:00remaining
How does the root AppModule affect global providers in a large NestJS application?

In a large NestJS app, what is the role of the root AppModule regarding global providers and their scope?

AGlobal providers declared in AppModule are only available to controllers, not services.
BGlobal providers must be declared in every feature module to be accessible.
CAppModule cannot declare global providers; only feature modules can.
DThe root AppModule can declare global providers that are shared across all modules without re-importing.
Attempts:
2 left
💡 Hint

Consider how the global scope works in NestJS modules.