CodePark/ questions / Modules & architecture
START PRACTICINGPRACTICE
Backend question · NestJS › Modules & architecture

UsersModule declares UsersService in providers but not in exports. OrdersModule imports UsersModule and injects UsersService into OrdersService. What happens?

Nest throws at startup that it can't resolve the dependencies of OrdersService

Nest throws an error at startup. Providers are encapsulated: importing a module only gives you what that module lists in exports. UsersService is not exported, so Nest stops during bootstrap with a "can't resolve dependencies" error.

"It works, because importing a module gives access to all its providers" is wrong, because importing only exposes exported providers. Nest also never creates a hidden second instance, and it never injects
undefined.