All quizzesMedium
Modules & Sharing
Preview — 3 of 10 questions
A UsersModule declares UsersService in providers but does not list it in exports. Another module imports UsersModule and tries to inject UsersService. What happens?
javascript
@Module({
providers: [UsersService],
exports: [UsersService], // ← required for other modules to inject it
})
export class UsersModule {}AIt silently injects undefined
BIt works, because importing a module exposes all of its providers
CNest throws an "Nest can't resolve dependencies" error because the provider was never exported
DIt works only if UsersService is @Global()
Why is a SharedModule commonly used in larger Nest applications?
javascript
@Module({
providers: [LoggerService],
exports: [LoggerService],
})
export class SharedModule {}ATo group commonly-used providers and re-export them so multiple feature modules can import one module instead of many
BTo make all providers globally available without importing anything
CTo bypass dependency injection entirely for shared state
DTo store environment variables centrally
Two modules depend on each other's exports. How do you resolve the circular module dependency?
javascript
@Module({
imports: [forwardRef(() => AuthModule)],
})
export class UsersModule {}
@Module({
imports: [forwardRef(() => UsersModule)],
})
export class AuthModule {}AMerge them into one giant module — circular deps are forbidden
BUse @Global() on both modules
CWrap each module reference in forwardRef(() => OtherModule) inside both imports arrays
DAdd circular: true to the @Module metadata
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.