<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Joh Labs</title>
    <description>The latest articles on DEV Community by Joh Labs (@johlabs).</description>
    <link>https://dev.to/johlabs</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4267722%2Fa5ee1440-ef5a-48e4-aaa8-22f688568e23.png</url>
      <title>DEV Community: Joh Labs</title>
      <link>https://dev.to/johlabs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/johlabs"/>
    <language>en</language>
    <item>
      <title>Catching "Nest can't resolve dependencies" without running the app</title>
      <dc:creator>Joh Labs</dc:creator>
      <pubDate>Thu, 26 Nov 2026 21:21:08 +0000</pubDate>
      <link>https://dev.to/johlabs/catching-nest-cant-resolve-dependencies-without-running-the-app-3l5o</link>
      <guid>https://dev.to/johlabs/catching-nest-cant-resolve-dependencies-without-running-the-app-3l5o</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;You know the one:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Nest can't resolve dependencies of the OrdersService (?).
Please make sure that the argument UsersService at index [0]
is available in the OrdersModule context.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here's what I learned about why it happens, and how you can check for it without running anything.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rule behind the error
&lt;/h2&gt;

&lt;p&gt;Take this setup:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// 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) {}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;OrdersModule&lt;/code&gt; imports &lt;code&gt;UsersModule&lt;/code&gt;, so it looks like it should work. It doesn't, because importing a module only gives you what that module &lt;strong&gt;exports&lt;/strong&gt;. &lt;code&gt;UsersModule&lt;/code&gt; provides &lt;code&gt;UsersService&lt;/code&gt; but never exports it.&lt;/p&gt;

&lt;p&gt;Inside a module, a class can inject something if:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;it's in that module's own &lt;code&gt;providers&lt;/code&gt;, or&lt;/li&gt;
&lt;li&gt;a module it imports lists it in &lt;code&gt;exports&lt;/code&gt;, or&lt;/li&gt;
&lt;li&gt;an imported module exports &lt;em&gt;another module&lt;/em&gt;, which re-exports everything that module exports (this chains), or&lt;/li&gt;
&lt;li&gt;it's exported by a module marked &lt;code&gt;@Global()&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's the whole rule. Everything else is detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking it from source
&lt;/h2&gt;

&lt;p&gt;Because the rule is that simple, you can check it by reading files:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find every &lt;code&gt;@Module()&lt;/code&gt; and read its &lt;code&gt;imports&lt;/code&gt;, &lt;code&gt;providers&lt;/code&gt; and &lt;code&gt;exports&lt;/code&gt; arrays.&lt;/li&gt;
&lt;li&gt;For each module, work out its &lt;strong&gt;visible set&lt;/strong&gt;: its own providers, plus the exports of everything it imports (following re-exported modules recursively), plus anything exported by a global module.&lt;/li&gt;
&lt;li&gt;For each class in that module's &lt;code&gt;providers&lt;/code&gt; or &lt;code&gt;controllers&lt;/code&gt;, 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.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The useful part is that you also know &lt;em&gt;how to fix it&lt;/em&gt;, because you can see where the class is provided. If &lt;code&gt;UsersService&lt;/code&gt; is provided in &lt;code&gt;UsersModule&lt;/code&gt; but not exported, the fix is "export it from &lt;code&gt;UsersModule&lt;/code&gt;". If it's already exported, the fix is "import &lt;code&gt;UsersModule&lt;/code&gt; into &lt;code&gt;OrdersModule&lt;/code&gt;". If nobody provides it, the fix is "add it to &lt;code&gt;providers&lt;/code&gt;".&lt;/p&gt;

&lt;h2&gt;
  
  
  The hard part: knowing when to stay quiet
&lt;/h2&gt;

&lt;p&gt;A checker that is wrong even occasionally gets switched off, so most of the work went into deciding what &lt;em&gt;not&lt;/em&gt; to report:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic modules.&lt;/strong&gt; &lt;code&gt;TypeOrmModule.forFeature()&lt;/code&gt;, &lt;code&gt;JwtModule.register()&lt;/code&gt; 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.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Library classes.&lt;/strong&gt; If the injected type comes from &lt;code&gt;node_modules&lt;/code&gt;, you can't see its module wiring, so it's skipped.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;@Optional()&lt;/code&gt;.&lt;/strong&gt; Optional dependencies don't fail, so they're skipped too.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Custom tokens.&lt;/strong&gt; &lt;code&gt;@Inject('CONFIG')&lt;/code&gt; or &lt;code&gt;@Inject(SOME_TOKEN)&lt;/code&gt; need the token resolved, not the parameter's type.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rule is to only report a missing provider when every module that could supply it is visible in the source.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: circular imports
&lt;/h2&gt;

&lt;p&gt;The same module graph gives you circular import detection almost for free. Run a strongly connected components pass (Tarjan's algorithm) over the &lt;code&gt;imports&lt;/code&gt; 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 &lt;code&gt;UsersModule → AuthModule → UsersModule&lt;/code&gt;, instead of a list of every module involved.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where this ended up
&lt;/h2&gt;

&lt;p&gt;I turned this into a plugin for WebStorm and IntelliJ called &lt;a href="https://plugins.jetbrains.com/plugin/36382-nest-assist/" rel="noopener noreferrer"&gt;Nest Assist&lt;/a&gt;. 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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>jetbrains</category>
    </item>
  </channel>
</rss>
