Catching "Nest can't resolve dependencies" without running the app
I'm a Salesforce developer, and I build IDE plugins on the side. When I went looking for a framework to build my first public one for, NestJS stood out for one reason: its most common error is completely predictable from
I'm a Salesforce developer, and I build IDE plugins on the side. When I went looking for a framework to build my first public one for, NestJS stood out for one reason: its most common error is completely predictable from the source code, but you only find out about it when the app starts.
You know the one:
Nest can't resolve dependencies of the OrdersService (?).
Please make sure that the argument UsersService at index [0]
is available in the OrdersModule context.
Here's what I learned about why it happens, and how you can check for it without running anything.
The rule behind the error
Take this setup:
// users.module.ts
@Module({
providers: [UsersService],
})
export class UsersModule {}
// orders.module.ts
@Module({
imports: [UsersModule],
providers: [OrdersService],
})
export class OrdersModule {}
// orders.service.ts
@Injectable()
export class OrdersService {
constructor(private users: UsersService) {}
}
OrdersModule imports UsersModule, so it looks like it should work. It doesn't, because importing a module only gives you what that module exports. UsersModule provides UsersService but never exports it.
Inside a module, a class can inject something if:
- it's in that module's own
providers, or - a module it imports lists it in
exports, or - an imported module exports another module, which re-exports everything that module exports (this chains), or
- it's exported by a module marked
@Global().
That's the whole rule. Everything else is detail.
Checking it from source
Because the rule is that simple, you can check it by reading files:
- Find every
@Module()and read itsimports,providersandexportsarrays. - For each module, work out its visible set: its own providers, plus the exports of everything it imports (following re-exported modules recursively), plus anything exported by a global module.
- For each class in that module's
providersorcontrollers, look at its constructor parameters. If a parameter's type isn't in the visible set, that's the error Nest would throw at startup.
The useful part is that you also know how to fix it, because you can see where the class is provided. If UsersService is provided in UsersModule but not exported, the fix is "export it from UsersModule". If it's already exported, the fix is "import UsersModule into OrdersModule". If nobody provides it, the fix is "add it to providers".
The hard part: knowing when to stay quiet
A checker that is wrong even occasionally gets switched off, so most of the work went into deciding what not to report:
-
Dynamic modules.
TypeOrmModule.forFeature(),JwtModule.register()and friends build their exports at runtime. If a module imports one of those, and the missing class could plausibly come from it, the check stays silent. -
Library classes. If the injected type comes from
node_modules, you can't see its module wiring, so it's skipped. -
@Optional(). Optional dependencies don't fail, so they're skipped too. -
Custom tokens.
@Inject('CONFIG')or@Inject(SOME_TOKEN)need the token resolved, not the parameter's type.
The rule is to only report a missing provider when every module that could supply it is visible in the source.
Bonus: circular imports
The same module graph gives you circular import detection almost for free. Run a strongly connected components pass (Tarjan's algorithm) over the imports graph. Any component with more than one module is a cycle. Then a breadth-first search inside that component gives you the shortest loop to show the user, like UsersModule → AuthModule → UsersModule, instead of a list of every module involved.
Where this ended up
I turned this into a plugin for WebStorm and IntelliJ called Nest Assist. The free version gives you a route list, go-to-handler by URL and a module graph. The paid version runs checks like the one above in the editor, with quick fixes.
I don't use Nest day to day, so I'm sure real projects have setups I haven't thought of. If you know a wiring pattern that would trip up a check like this, I'd really like to hear about it in the comments.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.