[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"verticals":3,"article-nestjs-weekly-invalid-enhancer-exception":44,"search-suggestions":474,"path-membership-nestjs-weekly-invalid-enhancer-exception":518,"related-nestjs-weekly-invalid-enhancer-exception":519,"code:ts:true:xvyyk":625,"code:ts:true:eiiu8l":626,"code:ts:true:11w1rwj":627,"code:plain:true:198lk1t":628,"code:ts:true:14rwibn":629,"code:ts:true:k6uysz":630,"comments-01a1fce0-e8b7-75e3-a229-fcaf96336655":631},[4,20,32],{"id":5,"slug":6,"name":7,"tagline":8,"description":9,"accentFrom":10,"accentTo":11,"icon":12,"defaultLocale":13,"locales":14,"features":16,"position":19},"019fe637-3d33-714b-b57f-23e163ffca0c","dev","Web Development","Read it. Run it. Prove it.","A post a day on modern web development — most with an editable playground and a quiz that explains every answer. Free, no account needed.","violet-500","cyan-400","◇","en",[13,15],"fa",{"courses":17,"paths":17,"articles":17,"exams":18,"flashcards":18,"packages":17,"community":17,"certificates":17,"teams":17,"commerce":17},true,false,0,{"id":21,"slug":22,"name":23,"tagline":24,"description":25,"accentFrom":26,"accentTo":10,"icon":27,"defaultLocale":13,"locales":28,"features":30,"position":31},"019fe637-3dc2-754c-8657-0f175bfee7c6","lang","Languages","Learn a language the way you learn a codebase.","Grammar explained the way good documentation explains an API — one idea at a time, each with a quiz.","amber-400","⌘",[13,15,29],"es",{"courses":18,"paths":18,"articles":17,"exams":18,"flashcards":17,"packages":18,"community":17,"certificates":17,"teams":18,"commerce":18},2,{"id":33,"slug":34,"name":35,"tagline":36,"description":37,"accentFrom":38,"accentTo":39,"icon":40,"defaultLocale":13,"locales":41,"features":42,"position":43},"7b3c16f2-931d-410e-802e-e1fa4edab7de","soft","Soft Skills","The half of the job nobody wrote documentation for.","Weekly, on the parts of working life that decide more than your code does — first weeks, meetings, interviews, promotions, and the people around you. Written from what actually happens, and recorded as a podcast you can listen to on the walk.","emerald-400","teal-300","◉",[13],{"courses":18,"paths":18,"articles":17,"exams":18,"flashcards":18,"packages":18,"community":17,"certificates":18,"teams":18,"commerce":18},3,{"id":45,"slug":46,"title":47,"subtitle":48,"excerpt":49,"coverUrl":50,"locale":13,"readingMinutes":51,"publishedAt":52,"viewCount":53,"likeCount":19,"commentCount":19,"author":54,"vertical":59,"topic":60,"tags":63,"_count":74,"playground":76,"body":78,"bodyMd":402,"seo":403,"translationGroupId":405,"series":406,"podcastUrl":48,"verticalId":5,"thread":462,"assessments":464,"translations":470,"quiz":472},"01a1fce0-e8b7-75e3-a229-fcaf96336655","nestjs-weekly-invalid-enhancer-exception","NestJS 12.5 InvalidEnhancerException: Why Guards Fail at Startup",null,"NestJS 12.5 throws InvalidEnhancerException when a guard, interceptor, or pipe can't run, closing a silent fail-open bug. Includes a copy-paste cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-invalid-enhancer-exception.png",15,"2026-12-04T21:45:21.140Z",70,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},"019fe637-3c25-7088-9034-39c9f15dc3c8","Parsa Jiravand","parsa","Frontend engineer · building bestpractic",{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},"nestjs","Nestjs",[64,65,68,71],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},"typescript","Typescript",{"slug":69,"name":70,"color":48},"security","Security",{"slug":72,"name":73,"color":48},"tutorial","Tutorial",{"assessments":75},1,{"slug":46,"title":77},"NestJS APP_GUARD resolution — InvalidEnhancerException model",{"blocks":79,"version":75},[80,84,87,92,95,104,107,110,113,116,130,133,136,142,145,148,151,154,157,160,163,166,169,173,176,179,182,185,189,192,195,198,202,205,210,213,216,223,226,229,233,236,239,242,245,248,251,254,261,264,272,275,278,281,285,288,291,294,297,300,303,306,309,348,352,355,358,361,364,367,370,378,381,384,387,390,393,396],{"id":81,"html":82,"type":83},"b1","\u003Cp>A team binds its \u003Ccode>AuthGuard\u003C\u002Fcode> globally: \u003Ccode>{ provide: APP_GUARD, useValue: AuthGuard }\u003C\u002Fcode>. It compiles. It passes code review — \u003Ccode>useValue\u003C\u002Fcode> and \u003Ccode>useClass\u003C\u002Fcode> look interchangeable at a glance, and TypeScript doesn&#39;t flag the typo, because both are legal shapes for a provider. The app boots cleanly. Every protected route keeps returning data, including to a request with no \u003Ccode>Authorization\u003C\u002Fcode> header at all, because the guard that was supposed to block it never ran — and nothing in the logs says so.\u003C\u002Fp>","paragraph",{"id":85,"html":86,"type":83},"b2","\u003Cp>This article is about \u003Ccode>InvalidEnhancerException\u003C\u002Fcode>, the error NestJS 12.5 added for exactly this class of bug: a guard, interceptor, or pipe that is bound but can&#39;t actually run. Before 12.5, Nest silently dropped it from the request pipeline. Since 12.5, it crashes your app at startup instead — loud, specific, and in your terminal before a single request ever reaches it.\u003C\u002Fp>",{"id":88,"html":89,"text":90,"type":91,"level":31},"b3","What you&#39;ll learn","What you'll learn","heading",{"id":93,"html":94,"type":83},"b4","\u003Cp>By the end of this article you&#39;ll be able to:\u003C\u002Fp>",{"id":96,"type":97,"items":98,"ordered":18},"b5","list",[99,100,101,102,103],"Explain how Nest resolves a guard, interceptor, or pipe differently from an ordinary injected service, and why that difference is where this bug lives","Reproduce the exact \u003Ccode>useValue\u003C\u002Fcode>\u002F\u003Ccode>useClass\u003C\u002Fcode> mistake that made a guard silently vanish from the pipeline before NestJS 12.5","Read an \u003Ccode>InvalidEnhancerException\u003C\u002Fcode> message and fix the binding it&#39;s complaining about in one line","Name the other three places NestJS 12.5 added this same check, and the one enhancer kind it still doesn&#39;t cover","Decide how to safely upgrade to 12.5 without a pre-existing bug in your app turning into a boot-time crash you weren&#39;t expecting",{"id":105,"html":106,"text":106,"type":91,"level":31},"b6","Who this is for",{"id":108,"html":109,"type":83},"b7","\u003Cp>You&#39;ve bound at least one guard or interceptor with \u003Ccode>@UseGuards()\u003C\u002Fcode>, \u003Ccode>@UseInterceptors()\u003C\u002Fcode>, or the \u003Ccode>APP_GUARD\u003C\u002Fcode>\u002F\u003Ccode>APP_INTERCEPTOR\u003C\u002Fcode>\u002F\u003Ccode>APP_PIPE\u003C\u002Fcode> tokens, and you&#39;re comfortable with constructor injection in Nest. If custom providers (\u003Ccode>useClass\u003C\u002Fcode>, \u003Ccode>useValue\u003C\u002Fcode>, \u003Ccode>useFactory\u003C\u002Fcode>) aren&#39;t second nature yet, this article defines each one as it uses it — you don&#39;t need to read anything else first.\u003C\u002Fp>",{"id":111,"html":112,"type":83},"b8","\u003Cp>This is written against \u003Cstrong>NestJS 12.x\u003C\u002Fstrong>, verified against \u003Ccode>@nestjs\u002Fcore\u003C\u002Fcode>&#39;s npm dist-tags on 2026-11-17: \u003Ccode>latest\u003C\u002Fcode> is \u003Cstrong>12.5.1\u003C\u002Fstrong> (released November 16, 2026), with \u003Cstrong>12.5.0\u003C\u002Fstrong> (November 12, 2026) the release that introduced \u003Ccode>InvalidEnhancerException\u003C\u002Fcode>; the 11.x line sits on the \u003Ccode>legacy\u003C\u002Fcode> tag at \u003Ccode>11.2.10\u003C\u002Fcode>. Both release dates and every behavior described below come from the \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fnestjs\u002Fnest\u002Freleases\u002Ftag\u002Fv12.5.0\">v12.5.0 GitHub release notes\u003C\u002Fa> and NestJS&#39;s own merged fix commits — never from memory.\u003C\u002Fp>",{"id":114,"html":115,"text":115,"type":91,"level":31},"b9","Table of contents",{"id":117,"type":97,"items":118,"ordered":18},"b10",[119,120,121,122,123,124,125,126,127,128,129],"\u003Ca href=\"#the-problem-a-guard-that-silently-never-ran\">The problem: a guard that silently never ran\u003C\u002Fa>","\u003Ca href=\"#the-mental-model-enhancers-are-providers-with-a-shape-contract\">The mental model: enhancers are providers with a shape contract\u003C\u002Fa>","\u003Ca href=\"#stage-1-binding-a-guard-correctly-with-useclass\">Stage 1: binding a guard correctly, with useClass\u003C\u002Fa>","\u003Ca href=\"#stage-2-the-bug--usevalue-an-unclear-factory-and-an-abstract-token\">Stage 2: the bug — useValue, an unclear factory, and an abstract token\u003C\u002Fa>","\u003Ca href=\"#stage-3-nestjs-125s-invalidenhancerexception\">Stage 3: NestJS 12.5&#39;s InvalidEnhancerException\u003C\u002Fa>","\u003Ca href=\"#stage-4-the-other-half--forroutes-and-exclude-validation\">Stage 4: the other half — forRoutes() and exclude() validation\u003C\u002Fa>","\u003Ca href=\"#edge-cases-and-gotchas\">Edge cases and gotchas\u003C\u002Fa>","\u003Ca href=\"#best-practices\">Best practices\u003C\u002Fa>","\u003Ca href=\"#faq\">FAQ\u003C\u002Fa>","\u003Ca href=\"#cheat-sheet\">Cheat sheet\u003C\u002Fa>","\u003Ca href=\"#key-takeaways\">Key takeaways\u003C\u002Fa>",{"id":131,"html":132,"text":132,"type":91,"level":31},"b11","The problem: a guard that silently never ran",{"id":134,"html":135,"type":83},"b12","\u003Cp>Here&#39;s the setup, reduced to the smallest form that reproduces it:\u003C\u002Fp>",{"id":137,"code":138,"type":139,"language":140,"highlight":141},"b13","\u002F\u002F auth.guard.ts\n@Injectable()\nexport class AuthGuard implements CanActivate {\n  canActivate(context: ExecutionContext): boolean {\n    const req = context.switchToHttp().getRequest();\n    return Boolean(req.headers.authorization);\n  }\n}\n\n\u002F\u002F app.module.ts — the bug\n@Module({\n  providers: [\n    AuthGuard,\n    { provide: APP_GUARD, useValue: AuthGuard }, \u002F\u002F should be useClass\n  ],\n})\nexport class AppModule {}","code","ts",[],{"id":143,"html":144,"type":83},"b14","\u003Cp>\u003Ccode>useValue\u003C\u002Fcode> and \u003Ccode>useClass\u003C\u002Fcode> are both one-line provider definitions, both compile, and both read as &quot;register \u003Ccode>AuthGuard\u003C\u002Fcode> as the global guard&quot; if you&#39;re skimming. They are not the same instruction. \u003Ccode>useClass: AuthGuard\u003C\u002Fcode> tells Nest&#39;s container: \u003Cem>instantiate this class, resolving its own constructor dependencies.\u003C\u002Fem> \u003Ccode>useValue: AuthGuard\u003C\u002Fcode> tells the container: \u003Cem>use this exact value as the provider, with no instantiation at all.\u003C\u002Fem> The value you handed it here isn&#39;t an \u003Ccode>AuthGuard\u003C\u002Fcode> instance — it&#39;s the \u003Ccode>AuthGuard\u003C\u002Fcode> \u003Cstrong>class itself\u003C\u002Fstrong>, a function. A function has no \u003Ccode>canActivate\u003C\u002Fcode> method sitting on it as an own property. It never did.\u003C\u002Fp>",{"id":146,"html":147,"type":83},"b15","\u003Cp>Before NestJS 12.5, here is exactly what happened next: Nest resolved the \u003Ccode>APP_GUARD\u003C\u002Fcode> provider, got back the \u003Ccode>AuthGuard\u003C\u002Fcode> constructor function, checked it for a \u003Ccode>canActivate\u003C\u002Fcode> method, found none — and quietly left it out of the guards array for every route it was supposed to protect. No exception. No log line. No different behavior at the call site. The app booted exactly as if the guard had never been registered, and every route it was meant to gate now accepts every request.\u003C\u002Fp>",{"id":149,"html":150,"text":150,"type":91,"level":31},"b16","The mental model: enhancers are providers with a shape contract",{"id":152,"html":153,"type":83},"b17","\u003Cp>Nest calls guards, interceptors, and pipes \u003Cstrong>enhancers\u003C\u002Fstrong>. The key fact that makes this bug possible — and that makes the fix make sense — is that an enhancer is resolved through the exact same dependency injection container as every other provider, but it&#39;s then used for something a normal provider is never checked for: calling a specific method on it, by name, on your behalf.\u003C\u002Fp>",{"id":155,"html":156,"type":83},"b18","\u003Cp>When you inject an ordinary service, TypeScript&#39;s type checker is doing real work: if your service doesn&#39;t implement the interface the consumer expects, the build fails before Nest ever runs. Enhancers partly escape that guarantee. \u003Ccode>APP_GUARD\u003C\u002Fcode>, \u003Ccode>APP_INTERCEPTOR\u003C\u002Fcode>, and \u003Ccode>APP_PIPE\u003C\u002Fcode> are plain injection tokens — symbols with no compile-time shape attached — because that&#39;s exactly what makes them flexible: \u003Ccode>useFactory\u003C\u002Fcode> can build one conditionally, \u003Ccode>useValue\u003C\u002Fcode> can swap in a test double, \u003Ccode>useExisting\u003C\u002Fcode> can alias one provider as another. The DI container doesn&#39;t know, and structurally \u003Cem>can&#39;t\u003C\u002Fem> know at compile time, that whatever comes out the other end needs a \u003Ccode>canActivate\u003C\u002Fcode>, an \u003Ccode>intercept\u003C\u002Fcode>, or a \u003Ccode>transform\u003C\u002Fcode> method. That check has to happen at runtime, against the actual resolved value — which is precisely the gap this bug lived in.\u003C\u002Fp>",{"id":158,"html":159,"type":83},"b19","\u003Cp>\u003Cstrong>The mental model:\u003C\u002Fstrong> think of enhancer resolution as two separate steps that used to only half-exist. Step one is ordinary DI: the container resolves whatever \u003Ccode>APP_GUARD\u003C\u002Fcode> (or \u003Ccode>@UseGuards(SomeGuard)\u003C\u002Fcode>) points to, exactly the way it resolves any other provider — by token, respecting scope, running factories, instantiating classes. Step two is new as of 12.5: once resolved, Nest calls a helper, \u003Ccode>assertEnhancer\u003C\u002Fcode>, that checks the resolved value actually has the method the role requires — \u003Ccode>canActivate\u003C\u002Fcode> for a guard, \u003Ccode>intercept\u003C\u002Fcode> for an interceptor, \u003Ccode>transform\u003C\u002Fcode> for a pipe — as a \u003Cstrong>callable property\u003C\u002Fstrong>, not just any truthy value. Before 12.5, step two didn&#39;t exist for most binding paths; a shape mismatch just made the enhancer get filtered out of the array Nest builds for a route, silently. Since 12.5, step two throws.\u003C\u002Fp>",{"id":161,"html":162,"type":83},"b20","\u003Cp>This same two-step idea is also why scope matters here, and it&#39;s worth tying back to what a singleton really means in Nest: an \u003Ccode>APP_GUARD\u003C\u002Fcode> bound with \u003Ccode>useClass\u003C\u002Fcode> is, by default, resolved \u003Cstrong>once\u003C\u002Fstrong>, at bootstrap, and that one instance is shared across every request — which is also exactly why the bootstrap-time check in 12.5 can catch the mistake before a single request arrives, instead of only during whichever request happens to trigger it.\u003C\u002Fp>",{"id":164,"html":165,"text":165,"type":91,"level":31},"b21","Stage 1: binding a guard correctly, with useClass",{"id":167,"html":168,"type":83},"b22","\u003Cp>The fix for the opening example is one word:\u003C\u002Fp>",{"id":170,"code":171,"type":139,"language":140,"highlight":172},"b23","@Module({\n  providers: [\n    AuthGuard,\n    { provide: APP_GUARD, useClass: AuthGuard }, \u002F\u002F instantiates AuthGuard via DI\n  ],\n})\nexport class AppModule {}",[],{"id":174,"html":175,"type":83},"b24","\u003Cp>\u003Ccode>useClass: AuthGuard\u003C\u002Fcode> tells the container to construct an \u003Ccode>AuthGuard\u003C\u002Fcode> instance, resolving any constructor dependencies \u003Ccode>AuthGuard\u003C\u002Fcode> declares along the way, exactly as it would for a controller&#39;s own dependencies. The resolved value is a real \u003Ccode>AuthGuard\u003C\u002Fcode> instance, which does have \u003Ccode>canActivate\u003C\u002Fcode> as an own prototype method. \u003Ccode>assertEnhancer\u003C\u002Fcode> passes. The guard runs on every request, the way the code always looked like it would.\u003C\u002Fp>",{"id":177,"html":178,"text":178,"type":91,"level":31},"b25","Stage 2: the bug — useValue, an unclear factory, and an abstract token",{"id":180,"html":181,"type":83},"b26","\u003Cp>The \u003Ccode>useValue\u003C\u002Fcode> mistake above is one way to produce an enhancer with the wrong shape. NestJS 12.5&#39;s own fix commit names two more, and both are easy to write by accident:\u003C\u002Fp>",{"id":183,"html":184,"type":83},"b27","\u003Cp>\u003Cstrong>An uncalled guard factory.\u003C\u002Fstrong> A common pattern builds a guard dynamically, parameterized by roles:\u003C\u002Fp>",{"id":186,"code":187,"type":139,"language":140,"highlight":188},"b28","function RolesGuard(...roles: string[]) {\n  @Injectable()\n  class RolesGuardImpl implements CanActivate {\n    canActivate(context: ExecutionContext): boolean {\n      \u002F\u002F ...check roles...\n      return true;\n    }\n  }\n  return RolesGuardImpl;\n}\n\n@UseGuards(RolesGuard('admin')) \u002F\u002F correct — the factory is called, returning a class\n@UseGuards(RolesGuard)          \u002F\u002F bug — this is the factory function itself",[],{"id":190,"html":191,"type":83},"b29","\u003Cp>\u003Ccode>RolesGuard(&#39;admin&#39;)\u003C\u002Fcode> calls the factory and binds the \u003Cstrong>class it returns\u003C\u002Fstrong>. \u003Ccode>RolesGuard\u003C\u002Fcode> alone — a copy-paste slip when refactoring away the parameters — binds the \u003Cstrong>factory function itself\u003C\u002Fstrong>, which has no \u003Ccode>canActivate\u003C\u002Fcode> method, because it isn&#39;t a guard at all; it&#39;s the thing that builds one.\u003C\u002Fp>",{"id":193,"html":194,"type":83},"b30","\u003Cp>\u003Cstrong>An abstract class used as a token with nothing bound to it.\u003C\u002Fstrong> Using an abstract base class as an injection token, intending some other module to provide the real implementation, is a legitimate pattern for a service — but if no concrete provider is ever registered for that token and it&#39;s used directly as a guard, there&#39;s no implementation behind it to call \u003Ccode>canActivate\u003C\u002Fcode> on.\u003C\u002Fp>",{"id":196,"html":197,"type":83},"b31","\u003Cp>All three — \u003Ccode>useValue\u003C\u002Fcode> with a class reference, an uncalled factory, and an unbound abstract token — share the same underlying shape: the container resolves to \u003Cem>something\u003C\u002Fem>, but that something isn&#39;t an instance with the method Nest is about to call.\u003C\u002Fp>",{"id":199,"html":200,"text":201,"type":91,"level":31},"b32","Stage 3: NestJS 12.5&#39;s InvalidEnhancerException","Stage 3: NestJS 12.5's InvalidEnhancerException",{"id":203,"html":204,"type":83},"b33","\u003Cp>Upgrade the app above to \u003Ccode>@nestjs\u002Fcore@12.5.1\u003C\u002Fcode> without fixing the binding, and instead of a clean boot, you get this at startup:\u003C\u002Fp>",{"id":206,"code":207,"type":139,"language":208,"highlight":209},"b34","[Nest] ERROR [ExceptionHandler] InvalidEnhancerException: The guard\n(AuthGuard) doesn't provide the \"canActivate\" method, so it can't run\nand the handlers it is bound to would run without it.","plain",[],{"id":211,"html":212,"type":83},"b35","\u003Cp>\u003Ccode>InvalidEnhancerException\u003C\u002Fcode> (extending Nest&#39;s \u003Ccode>RuntimeException\u003C\u002Fcode>) is thrown by the new \u003Ccode>assertEnhancer\u003C\u002Fcode> helper, and per the fix&#39;s own test suite, the message names the enhancer kind, the class or token involved, and the exact method it&#39;s missing — \u003Ccode>doesn&#39;t provide the &quot;canActivate&quot; method\u003C\u002Fcode> for a guard, the equivalent for \u003Ccode>intercept\u003C\u002Fcode> on an interceptor or \u003Ccode>transform\u003C\u002Fcode> on a pipe. That&#39;s enough to go straight to the binding and fix it, without a debugger session.\u003C\u002Fp>",{"id":214,"html":215,"type":83},"b36","\u003Cp>The check isn&#39;t bolted on in one spot — it runs at four points in the resolution path, because an enhancer can be bound four different ways:\u003C\u002Fp>",{"id":217,"type":97,"items":218,"ordered":17},"b37",[219,220,221,222],"\u003Cstrong>\u003Ccode>app.useGlobalGuards()\u003C\u002Fcode> \u002F \u003Ccode>useGlobalInterceptors()\u003C\u002Fcode> \u002F \u003Ccode>useGlobalPipes()\u003C\u002Fcode>\u003C\u002Fstrong> — each argument is checked the moment you pass it in.","\u003Cstrong>\u003Ccode>@UseGuards()\u003C\u002Fcode> \u002F \u003Ccode>@UseInterceptors()\u003C\u002Fcode> \u002F \u003Ccode>@UseBindPipes()\u003C\u002Fcode>-style decorators at controller or method scope\u003C\u002Fstrong> — checked once Nest has constructed the bound class.","\u003Cstrong>Per-request resolution for request-scoped or transient enhancers\u003C\u002Fstrong> — checked each time a fresh instance is resolved for a request, since a different instance could theoretically fail where an earlier one passed.","\u003Cstrong>\u003Ccode>APP_GUARD\u003C\u002Fcode> \u002F \u003Ccode>APP_INTERCEPTOR\u003C\u002Fcode> \u002F \u003Ccode>APP_PIPE\u003C\u002Fcode> providers\u003C\u002Fstrong> — checked when the module that registers them finishes compiling, which is what caught the opening example at boot, before any route existed to call it.",{"id":224,"html":225,"text":225,"type":91,"level":31},"b38","Stage 4: the other half — forRoutes() and exclude() validation",{"id":227,"html":228,"type":83},"b39","\u003Cp>The same NestJS 12.5 release closed a parallel gap in \u003Ccode>RoutesMapper\u003C\u002Fcode>, the piece that resolves \u003Ccode>MiddlewareConsumer.forRoutes()\u003C\u002Fcode> and \u003Ccode>.exclude()\u003C\u002Fcode> entries for middleware. Previously, passing a class with no \u003Ccode>@Controller()\u003C\u002Fcode> decorator — a plain base class, say — silently mapped to a route with no path and no method, so the middleware matched nothing and never ran. A malformed route object (missing or non-numeric \u003Ccode>method\u003C\u002Fcode>) failed the same way. As of 12.5, both now throw \u003Ccode>InvalidMiddlewareConfigurationException\u003C\u002Fcode> at startup, naming the offending value, instead of quietly matching nothing:\u003C\u002Fp>",{"id":230,"code":231,"type":139,"language":140,"highlight":232},"b40","consumer\n  .apply(LoggerMiddleware)\n  .forRoutes(SomeBaseClassWithoutController); \u002F\u002F now throws at startup",[],{"id":234,"html":235,"type":83},"b41","\u003Cp>It&#39;s a different exception class from \u003Ccode>InvalidEnhancerException\u003C\u002Fcode>, because middleware target resolution is a separate code path from enhancer resolution — but it&#39;s the same underlying fix philosophy in the same release: a configuration mistake that used to disappear into &quot;matches nothing&quot; now surfaces as a startup failure you can&#39;t miss.\u003C\u002Fp>",{"id":237,"html":238,"type":83},"b42","\u003C!-- playground:start -->",{"id":240,"html":241,"text":241,"type":91,"level":31},"b43","🎮 Try it yourself",{"id":243,"html":244,"type":83},"b44","\u003Cp>\u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-invalid-enhancer-exception\u002Fplayground\">▶️ Open the interactive playground →\u003C\u002Fa>\u003C\u002Fstrong>\u003C\u002Fp>",{"id":246,"html":247,"type":83},"b45","\u003Cp>\u003Cem>Runs right in your browser — poke at it and watch the concept react live.\u003C\u002Fem>\u003C\u002Fp>",{"id":249,"html":250,"type":83},"b46","\u003C!-- playground:end -->",{"id":252,"html":253,"text":253,"type":91,"level":31},"b47","Edge cases and gotchas",{"id":255,"type":97,"items":256,"ordered":18},"b48",[257,258,259,260],"\u003Cstrong>Exception filters are the one enhancer kind this still doesn&#39;t cover.\u003C\u002Fstrong> A filter that can&#39;t run (missing \u003Ccode>catch()\u003C\u002Fcode>) only logs a warning — it doesn&#39;t throw. That&#39;s a real asymmetry in 12.5: don&#39;t assume every enhancer kind now fails loudly, and don&#39;t rely on a bad filter binding to crash your app the way a bad guard binding will.","\u003Cstrong>Request-scoped and transient enhancers get a pass in one specific case.\u003C\u002Fstrong> Nest builds lightweight placeholder instances for \u003Ccode>REQUEST\u003C\u002Fcode>- and \u003Ccode>TRANSIENT\u003C\u002Fcode>-scoped enhancers outside of an actual request context (for example, while wiring up the module graph). \u003Ccode>assertEnhancer\u003C\u002Fcode> skips those placeholders rather than failing on a stand-in that was never meant to be called directly — the real check still happens once a request resolves the real, per-request instance.","\u003Cstrong>A deliberately empty factory result is allowed.\u003C\u002Fstrong> If an \u003Ccode>APP_GUARD\u003C\u002Fcode>&#39;s \u003Ccode>useFactory\u003C\u002Fcode> is written to conditionally return nothing (\u003Ccode>() =&gt; (flag ? SomeGuard : undefined)\u003C\u002Fcode>), that&#39;s treated as &quot;no enhancer here&quot; and skipped, not an error — this is a known pattern for feature-flagging a global guard, and 12.5 preserves it.","\u003Cstrong>This upgrade can turn a dormant bug into a boot-time crash.\u003C\u002Fstrong> If your app already has a misconfigured enhancer like the ones above, it has been silently running unguarded, unintercepted, or unvalidated routes in production. Upgrading to 12.5 won&#39;t introduce the bug — it will surface one that was already there. Upgrade in staging first, specifically so you find out there, not when production refuses to start.",{"id":262,"html":263,"text":263,"type":91,"level":31},"b49","Best practices",{"id":265,"type":97,"items":266,"ordered":18},"b50",[267,268,269,270,271],"\u003Cstrong>Default to \u003Ccode>useClass\u003C\u002Fcode> for a guard, interceptor, or pipe bound through \u003Ccode>APP_GUARD\u003C\u002Fcode>\u002F\u003Ccode>APP_INTERCEPTOR\u003C\u002Fcode>\u002F\u003Ccode>APP_PIPE\u003C\u002Fcode>\u003C\u002Fstrong>, unless you specifically need \u003Ccode>useFactory\u003C\u002Fcode> for conditional construction — and if you do, make sure every branch returns either a real instance\u002Fclass or \u003Ccode>undefined\u003C\u002Fcode>, never the bare class reference by accident.","\u003Cstrong>Always call a guard\u002Finterceptor factory before binding it\u003C\u002Fstrong>: \u003Ccode>@UseGuards(RolesGuard(&#39;admin&#39;))\u003C\u002Fcode>, never \u003Ccode>@UseGuards(RolesGuard)\u003C\u002Fcode>. If you rename a factory to take no arguments, double-check every call site still invokes it.","\u003Cstrong>Don&#39;t lean on an abstract class as a directly-bound enhancer token\u003C\u002Fstrong> unless a concrete implementation is actually registered against it somewhere in the module graph.","\u003Cstrong>Treat exception filters with extra care during review\u003C\u002Fstrong>, since a misbound filter still won&#39;t tell you at startup — it only warns, and warnings get lost in noisy logs.","\u003Cstrong>Upgrade to 12.5 in staging first\u003C\u002Fstrong>, and treat any new \u003Ccode>InvalidEnhancerException\u003C\u002Fcode> or \u003Ccode>InvalidMiddlewareConfigurationException\u003C\u002Fcode> at boot as a genuine, pre-existing bug report — not a false positive to silence.",{"id":273,"html":274,"text":274,"type":91,"level":31},"b51","FAQ",{"id":276,"html":277,"text":277,"type":91,"level":43},"b52","Will upgrading to NestJS 12.5 break my working app?",{"id":279,"html":280,"type":83},"b53","\u003Cp>Only if an enhancer or a \u003Ccode>forRoutes()\u003C\u002Fcode>\u002F\u003Ccode>exclude()\u003C\u002Fcode> entry was already misconfigured in a way that made it silently do nothing. A correctly-bound guard, interceptor, or pipe behaves exactly as before — the new checks only fire on a shape that was always wrong.\u003C\u002Fp>",{"id":282,"html":283,"text":284,"type":91,"level":43},"b54","What&#39;s the actual difference between \u003Ccode>useValue\u003C\u002Fcode> and \u003Ccode>useClass\u003C\u002Fcode> here?","What's the actual difference between useValue and useClass here?",{"id":286,"html":287,"type":83},"b55","\u003Cp>\u003Ccode>useClass: SomeGuard\u003C\u002Fcode> tells Nest to construct a \u003Ccode>SomeGuard\u003C\u002Fcode> instance through the DI container, resolving its constructor dependencies. \u003Ccode>useValue: SomeGuard\u003C\u002Fcode> tells Nest to use the exact value you passed — in this case the class itself, not an instance of it — with no construction step at all. For a guard, you almost always want the constructed instance, so \u003Ccode>useClass\u003C\u002Fcode> (or a correctly-called factory) is the right call.\u003C\u002Fp>",{"id":289,"html":290,"text":290,"type":91,"level":43},"b56","Does this validation apply to exception filters too?",{"id":292,"html":293,"type":83},"b57","\u003Cp>No. Per the 12.5 release notes, a guard, interceptor, or pipe that can&#39;t run now fails the application at startup; a filter that can&#39;t run still only logs a warning. Review filter bindings with extra attention until that changes.\u003C\u002Fp>",{"id":295,"html":296,"text":296,"type":91,"level":43},"b58","Does a request-scoped guard get checked differently from a singleton one?",{"id":298,"html":299,"type":83},"b59","\u003Cp>The check is the same \u003Ccode>assertEnhancer\u003C\u002Fcode> call either way — it just runs at a different point. A singleton (the default scope) is checked once, when it&#39;s constructed at bootstrap. A \u003Ccode>REQUEST\u003C\u002Fcode>- or \u003Ccode>TRANSIENT\u003C\u002Fcode>-scoped enhancer is checked each time a real instance is resolved for an actual request; placeholder instances built outside a request context are skipped so they don&#39;t trip the check on a stand-in.\u003C\u002Fp>",{"id":301,"html":302,"text":302,"type":91,"level":43},"b60","Is this change itself a breaking change?",{"id":304,"html":305,"type":83},"b61","\u003Cp>NestJS ships it as a bug fix, not a new feature with an opt-out — a misconfigured enhancer silently doing nothing was never the intended behavior. But because it changes a previously-silent failure into a startup crash, treat upgrading like any fix that can change observable behavior: run it in staging and read every new boot-time exception before you deploy.\u003C\u002Fp>",{"id":307,"html":308,"text":308,"type":91,"level":31},"b62","Cheat sheet",{"id":310,"head":311,"rows":316,"type":347},"b63",[312,313,314,315],"Mistake","What happens pre-12.5","What happens on 12.5+","Fix",[317,322,327,332,337,342],[318,319,320,321],"\u003Ccode>{ provide: APP_GUARD, useValue: AuthGuard }\u003C\u002Fcode>","Guard silently dropped; routes run unguarded","\u003Ccode>InvalidEnhancerException\u003C\u002Fcode>: missing \u003Ccode>&quot;canActivate&quot;\u003C\u002Fcode>","\u003Ccode>{ provide: APP_GUARD, useClass: AuthGuard }\u003C\u002Fcode>",[323,324,325,326],"\u003Ccode>@UseGuards(RolesGuard)\u003C\u002Fcode> where \u003Ccode>RolesGuard\u003C\u002Fcode> is an uncalled factory","Guard silently dropped","\u003Ccode>InvalidEnhancerException\u003C\u002Fcode> on the bound class","\u003Ccode>@UseGuards(RolesGuard(&#39;admin&#39;))\u003C\u002Fcode> — call the factory",[328,329,330,331],"An interceptor\u002Fpipe bound the same way (\u003Ccode>useValue\u003C\u002Fcode> with a class, or uncalled factory)","Silently dropped from the pipeline","\u003Ccode>InvalidEnhancerException\u003C\u002Fcode>: missing \u003Ccode>&quot;intercept&quot;\u003C\u002Fcode>\u002F\u003Ccode>&quot;transform&quot;\u003C\u002Fcode>","Use \u003Ccode>useClass\u003C\u002Fcode>, or call the factory",[333,334,335,336],"\u003Ccode>useFactory\u003C\u002Fcode> that conditionally returns \u003Ccode>undefined\u003C\u002Fcode>","Skipped (intended, feature-flag pattern)","Still skipped — not an error","No change needed",[338,339,340,341],"Exception filter that doesn&#39;t implement \u003Ccode>catch()\u003C\u002Fcode>","Silently dropped","\u003Cstrong>Still only a warning, no throw\u003C\u002Fstrong>","Fix the filter; don&#39;t rely on a crash to catch it",[343,344,345,346],"\u003Ccode>.forRoutes(SomeClassWithoutController)\u003C\u002Fcode>","Middleware matches nothing, silently","\u003Ccode>InvalidMiddlewareConfigurationException\u003C\u002Fcode>","Add \u003Ccode>@Controller()\u003C\u002Fcode>, or pass a valid \u003Ccode>{ path, method }\u003C\u002Fcode> route object","table",{"id":349,"code":350,"type":139,"language":140,"highlight":351},"b64","\u002F\u002F The correct, DI-respecting way to bind every global enhancer\n@Module({\n  providers: [\n    AuthGuard,\n    LoggingInterceptor,\n    ValidationPipe,\n    { provide: APP_GUARD, useClass: AuthGuard },\n    { provide: APP_INTERCEPTOR, useClass: LoggingInterceptor },\n    { provide: APP_PIPE, useClass: ValidationPipe },\n  ],\n})\nexport class AppModule {}",[],{"id":353,"html":354,"type":83},"b65","\u003C!-- quiz:start -->",{"id":356,"html":357,"text":357,"type":91,"level":31},"b66","🧠 Test yourself",{"id":359,"html":360,"type":83},"b67","\u003Cp>Think it clicked? \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-invalid-enhancer-exception\u002Fquiz\">Take the 8-question quiz →\u003C\u002Fa>\u003C\u002Fstrong>\u003C\u002Fp>",{"id":362,"html":363,"type":83},"b68","\u003Cp>\u003Cem>Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.\u003C\u002Fem>\u003C\u002Fp>",{"id":365,"html":366,"type":83},"b69","\u003C!-- quiz:end -->",{"id":368,"html":369,"text":369,"type":91,"level":31},"b70","Key takeaways",{"id":371,"type":97,"items":372,"ordered":18},"b71",[373,374,375,376,377],"A guard, interceptor, or pipe is resolved through the same DI container as any provider, but then checked for a required method (\u003Ccode>canActivate\u003C\u002Fcode>, \u003Ccode>intercept\u003C\u002Fcode>, \u003Ccode>transform\u003C\u002Fcode>) that nothing at compile time enforces — that gap is where this bug lived.","Before NestJS 12.5, an enhancer with the wrong shape was silently dropped from the pipeline; the route it was bound to ran exactly as if the guard, interceptor, or pipe didn&#39;t exist.","\u003Ccode>InvalidEnhancerException\u003C\u002Fcode> (12.5+) throws at startup the moment a misconfigured guard, interceptor, or pipe is resolved — whether that&#39;s at \u003Ccode>app.useGlobal*()\u003C\u002Fcode>, a decorator, an \u003Ccode>APP_*\u003C\u002Fcode> provider, or per-request resolution for a scoped enhancer.","\u003Ccode>useValue: SomeClass\u003C\u002Fcode> is not the same as \u003Ccode>useClass: SomeClass\u003C\u002Fcode> — the first hands Nest the class itself with no instantiation, the second constructs a real instance through DI.","Exception filters are the one exception: a filter that can&#39;t run still only logs a warning. Upgrading to 12.5 won&#39;t fix that on its own, and it can turn a dormant guard\u002Finterceptor\u002Fpipe bug into a startup crash — upgrade in staging first.",{"id":379,"html":380,"type":83},"b72","\u003Cp>If your \u003Ccode>AuthGuard\u003C\u002Fcode> typo makes it to production today, nothing will tell you — the route will just work for everyone, guarded or not. As of NestJS 12.5, that same typo stops your app before it ever opens a port. That&#39;s a straightforward trade: a crash you see in CI beats a security hole you find in an incident review.\u003C\u002Fp>",{"id":382,"html":383,"type":83},"b73","\u003Cp>This series has already covered the two pieces this bug sits between: \u003Ca href=\"https:\u002F\u002Fdev.to\u002Fparsajiravand\u002Fnestjs-dependency-injection-explained-with-cheat-sheet-4bem\">how Nest&#39;s DI container resolves providers and scopes\u003C\u002Fa> in the first place, and \u003Ca href=\"https:\u002F\u002Fdev.to\u002Fparsajiravand\u002Fnestjs-guards-canactivate-executioncontext-reflector-1onm\">how a guard&#39;s \u003Ccode>canActivate\u003C\u002Fcode> actually gets called\u003C\u002Fa> once it&#39;s resolved correctly. If you write tests against provider overrides, \u003Ca href=\"https:\u002F\u002Fdev.to\u002Fparsajiravand\u002Fnestjs-testing-module-provider-overrides-with-cheat-sheet-1o79\">the testing module episode\u003C\u002Fa> covers the DI-aware way to swap a guard for a fake in a test — the same \u003Ccode>useClass\u003C\u002Fcode>\u002F\u003Ccode>useValue\u003C\u002Fcode> distinction this article turns on.\u003C\u002Fp>",{"id":385,"html":386,"type":83},"b74","\u003Cp>Has a provider binding typo ever shipped to production silently in one of your apps — and if so, what finally surfaced it: a log, a test, or a user?\u003C\u002Fp>",{"id":388,"type":389},"b75","divider",{"id":391,"html":392,"type":83},"b76","\u003Cp>🚀 \u003Cstrong>Want more like this?\u003C\u002Fstrong> Every guide, playground, and quiz lives on \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002F\">bestpractic.org\u003C\u002Fa>\u003C\u002Fstrong> — open it and \u003Cstrong>\u003Ca href=\"https:\u002F\u002Fbestpractic.org\u002F\">sign up free\u003C\u002Fa>\u003C\u002Fstrong> so the next one finds you.\u003C\u002Fp>",{"id":394,"html":395,"type":83},"b77","\u003Cp>\u003Cem>Thanks for reading! Let&#39;s stay connected:\u003C\u002Fem>\u003C\u002Fp>",{"id":397,"type":97,"items":398,"ordered":18},"b78",[399,400,401],"⭐ \u003Cstrong>GitHub\u003C\u002Fstrong> — follow me and star the projects: \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fparsajiravand\">github.com\u002Fparsajiravand\u003C\u002Fa>","💬 \u003Cstrong>Discord\u003C\u002Fstrong> — join the frontend best-practices community: \u003Ca href=\"https:\u002F\u002Fdiscord.gg\u002Fd9KRhuAwQ\">discord.gg\u002Fd9KRhuAwQ\u003C\u002Fa>","📸 \u003Cstrong>Instagram\u003C\u002Fstrong> — frontend best practices, daily: \u003Ca href=\"https:\u002F\u002Fwww.instagram.com\u002Fbestpractice___\u002F\">@bestpractice___\u003C\u002Fa>","A team binds its `AuthGuard` globally: `{ provide: APP_GUARD, useValue: AuthGuard }`. It compiles. It passes code review — `useValue` and `useClass` look interchangeable at a glance, and TypeScript doesn't flag the typo, because both are legal shapes for a provider. The app boots cleanly. Every protected route keeps returning data, including to a request with no `Authorization` header at all, because the guard that was supposed to block it never ran — and nothing in the logs says so.\n\nThis article is about `InvalidEnhancerException`, the error NestJS 12.5 added for exactly this class of bug: a guard, interceptor, or pipe that is bound but can't actually run. Before 12.5, Nest silently dropped it from the request pipeline. Since 12.5, it crashes your app at startup instead — loud, specific, and in your terminal before a single request ever reaches it.\n\n## What you'll learn\n\nBy the end of this article you'll be able to:\n\n- Explain how Nest resolves a guard, interceptor, or pipe differently from an ordinary injected service, and why that difference is where this bug lives\n- Reproduce the exact `useValue`\u002F`useClass` mistake that made a guard silently vanish from the pipeline before NestJS 12.5\n- Read an `InvalidEnhancerException` message and fix the binding it's complaining about in one line\n- Name the other three places NestJS 12.5 added this same check, and the one enhancer kind it still doesn't cover\n- Decide how to safely upgrade to 12.5 without a pre-existing bug in your app turning into a boot-time crash you weren't expecting\n\n## Who this is for\n\nYou've bound at least one guard or interceptor with `@UseGuards()`, `@UseInterceptors()`, or the `APP_GUARD`\u002F`APP_INTERCEPTOR`\u002F`APP_PIPE` tokens, and you're comfortable with constructor injection in Nest. If custom providers (`useClass`, `useValue`, `useFactory`) aren't second nature yet, this article defines each one as it uses it — you don't need to read anything else first.\n\nThis is written against **NestJS 12.x**, verified against `@nestjs\u002Fcore`'s npm dist-tags on 2026-11-17: `latest` is **12.5.1** (released November 16, 2026), with **12.5.0** (November 12, 2026) the release that introduced `InvalidEnhancerException`; the 11.x line sits on the `legacy` tag at `11.2.10`. Both release dates and every behavior described below come from the [v12.5.0 GitHub release notes](https:\u002F\u002Fgithub.com\u002Fnestjs\u002Fnest\u002Freleases\u002Ftag\u002Fv12.5.0) and NestJS's own merged fix commits — never from memory.\n\n## Table of contents\n\n- [The problem: a guard that silently never ran](#the-problem-a-guard-that-silently-never-ran)\n- [The mental model: enhancers are providers with a shape contract](#the-mental-model-enhancers-are-providers-with-a-shape-contract)\n- [Stage 1: binding a guard correctly, with useClass](#stage-1-binding-a-guard-correctly-with-useclass)\n- [Stage 2: the bug — useValue, an unclear factory, and an abstract token](#stage-2-the-bug--usevalue-an-unclear-factory-and-an-abstract-token)\n- [Stage 3: NestJS 12.5's InvalidEnhancerException](#stage-3-nestjs-125s-invalidenhancerexception)\n- [Stage 4: the other half — forRoutes() and exclude() validation](#stage-4-the-other-half--forroutes-and-exclude-validation)\n- [Edge cases and gotchas](#edge-cases-and-gotchas)\n- [Best practices](#best-practices)\n- [FAQ](#faq)\n- [Cheat sheet](#cheat-sheet)\n- [Key takeaways](#key-takeaways)\n\n## The problem: a guard that silently never ran\n\nHere's the setup, reduced to the smallest form that reproduces it:\n\n```typescript\n\u002F\u002F auth.guard.ts\n@Injectable()\nexport class AuthGuard implements CanActivate {\n  canActivate(context: ExecutionContext): boolean {\n    const req = context.switchToHttp().getRequest();\n    return Boolean(req.headers.authorization);\n  }\n}\n\n\u002F\u002F app.module.ts — the bug\n@Module({\n  providers: [\n    AuthGuard,\n    { provide: APP_GUARD, useValue: AuthGuard }, \u002F\u002F should be useClass\n  ],\n})\nexport class AppModule {}\n```\n\n`useValue` and `useClass` are both one-line provider definitions, both compile, and both read as \"register `AuthGuard` as the global guard\" if you're skimming. They are not the same instruction. `useClass: AuthGuard` tells Nest's container: *instantiate this class, resolving its own constructor dependencies.* `useValue: AuthGuard` tells the container: *use this exact value as the provider, with no instantiation at all.* The value you handed it here isn't an `AuthGuard` instance — it's the `AuthGuard` **class itself**, a function. A function has no `canActivate` method sitting on it as an own property. It never did.\n\nBefore NestJS 12.5, here is exactly what happened next: Nest resolved the `APP_GUARD` provider, got back the `AuthGuard` constructor function, checked it for a `canActivate` method, found none — and quietly left it out of the guards array for every route it was supposed to protect. No exception. No log line. No different behavior at the call site. The app booted exactly as if the guard had never been registered, and every route it was meant to gate now accepts every request.\n\n## The mental model: enhancers are providers with a shape contract\n\nNest calls guards, interceptors, and pipes **enhancers**. The key fact that makes this bug possible — and that makes the fix make sense — is that an enhancer is resolved through the exact same dependency injection container as every other provider, but it's then used for something a normal provider is never checked for: calling a specific method on it, by name, on your behalf.\n\nWhen you inject an ordinary service, TypeScript's type checker is doing real work: if your service doesn't implement the interface the consumer expects, the build fails before Nest ever runs. Enhancers partly escape that guarantee. `APP_GUARD`, `APP_INTERCEPTOR`, and `APP_PIPE` are plain injection tokens — symbols with no compile-time shape attached — because that's exactly what makes them flexible: `useFactory` can build one conditionally, `useValue` can swap in a test double, `useExisting` can alias one provider as another. The DI container doesn't know, and structurally *can't* know at compile time, that whatever comes out the other end needs a `canActivate`, an `intercept`, or a `transform` method. That check has to happen at runtime, against the actual resolved value — which is precisely the gap this bug lived in.\n\n**The mental model:** think of enhancer resolution as two separate steps that used to only half-exist. Step one is ordinary DI: the container resolves whatever `APP_GUARD` (or `@UseGuards(SomeGuard)`) points to, exactly the way it resolves any other provider — by token, respecting scope, running factories, instantiating classes. Step two is new as of 12.5: once resolved, Nest calls a helper, `assertEnhancer`, that checks the resolved value actually has the method the role requires — `canActivate` for a guard, `intercept` for an interceptor, `transform` for a pipe — as a **callable property**, not just any truthy value. Before 12.5, step two didn't exist for most binding paths; a shape mismatch just made the enhancer get filtered out of the array Nest builds for a route, silently. Since 12.5, step two throws.\n\nThis same two-step idea is also why scope matters here, and it's worth tying back to what a singleton really means in Nest: an `APP_GUARD` bound with `useClass` is, by default, resolved **once**, at bootstrap, and that one instance is shared across every request — which is also exactly why the bootstrap-time check in 12.5 can catch the mistake before a single request arrives, instead of only during whichever request happens to trigger it.\n\n## Stage 1: binding a guard correctly, with useClass\n\nThe fix for the opening example is one word:\n\n```typescript\n@Module({\n  providers: [\n    AuthGuard,\n    { provide: APP_GUARD, useClass: AuthGuard }, \u002F\u002F instantiates AuthGuard via DI\n  ],\n})\nexport class AppModule {}\n```\n\n`useClass: AuthGuard` tells the container to construct an `AuthGuard` instance, resolving any constructor dependencies `AuthGuard` declares along the way, exactly as it would for a controller's own dependencies. The resolved value is a real `AuthGuard` instance, which does have `canActivate` as an own prototype method. `assertEnhancer` passes. The guard runs on every request, the way the code always looked like it would.\n\n## Stage 2: the bug — useValue, an unclear factory, and an abstract token\n\nThe `useValue` mistake above is one way to produce an enhancer with the wrong shape. NestJS 12.5's own fix commit names two more, and both are easy to write by accident:\n\n**An uncalled guard factory.** A common pattern builds a guard dynamically, parameterized by roles:\n\n```typescript\nfunction RolesGuard(...roles: string[]) {\n  @Injectable()\n  class RolesGuardImpl implements CanActivate {\n    canActivate(context: ExecutionContext): boolean {\n      \u002F\u002F ...check roles...\n      return true;\n    }\n  }\n  return RolesGuardImpl;\n}\n\n@UseGuards(RolesGuard('admin')) \u002F\u002F correct — the factory is called, returning a class\n@UseGuards(RolesGuard)          \u002F\u002F bug — this is the factory function itself\n```\n\n`RolesGuard('admin')` calls the factory and binds the **class it returns**. `RolesGuard` alone — a copy-paste slip when refactoring away the parameters — binds the **factory function itself**, which has no `canActivate` method, because it isn't a guard at all; it's the thing that builds one.\n\n**An abstract class used as a token with nothing bound to it.** Using an abstract base class as an injection token, intending some other module to provide the real implementation, is a legitimate pattern for a service — but if no concrete provider is ever registered for that token and it's used directly as a guard, there's no implementation behind it to call `canActivate` on.\n\nAll three — `useValue` with a class reference, an uncalled factory, and an unbound abstract token — share the same underlying shape: the container resolves to *something*, but that something isn't an instance with the method Nest is about to call.\n\n## Stage 3: NestJS 12.5's InvalidEnhancerException\n\nUpgrade the app above to `@nestjs\u002Fcore@12.5.1` without fixing the binding, and instead of a clean boot, you get this at startup:\n\n```\n[Nest] ERROR [ExceptionHandler] InvalidEnhancerException: The guard\n(AuthGuard) doesn't provide the \"canActivate\" method, so it can't run\nand the handlers it is bound to would run without it.\n```\n\n`InvalidEnhancerException` (extending Nest's `RuntimeException`) is thrown by the new `assertEnhancer` helper, and per the fix's own test suite, the message names the enhancer kind, the class or token involved, and the exact method it's missing — `doesn't provide the \"canActivate\" method` for a guard, the equivalent for `intercept` on an interceptor or `transform` on a pipe. That's enough to go straight to the binding and fix it, without a debugger session.\n\nThe check isn't bolted on in one spot — it runs at four points in the resolution path, because an enhancer can be bound four different ways:\n\n1. **`app.useGlobalGuards()` \u002F `useGlobalInterceptors()` \u002F `useGlobalPipes()`** — each argument is checked the moment you pass it in.\n2. **`@UseGuards()` \u002F `@UseInterceptors()` \u002F `@UseBindPipes()`-style decorators at controller or method scope** — checked once Nest has constructed the bound class.\n3. **Per-request resolution for request-scoped or transient enhancers** — checked each time a fresh instance is resolved for a request, since a different instance could theoretically fail where an earlier one passed.\n4. **`APP_GUARD` \u002F `APP_INTERCEPTOR` \u002F `APP_PIPE` providers** — checked when the module that registers them finishes compiling, which is what caught the opening example at boot, before any route existed to call it.\n\n## Stage 4: the other half — forRoutes() and exclude() validation\n\nThe same NestJS 12.5 release closed a parallel gap in `RoutesMapper`, the piece that resolves `MiddlewareConsumer.forRoutes()` and `.exclude()` entries for middleware. Previously, passing a class with no `@Controller()` decorator — a plain base class, say — silently mapped to a route with no path and no method, so the middleware matched nothing and never ran. A malformed route object (missing or non-numeric `method`) failed the same way. As of 12.5, both now throw `InvalidMiddlewareConfigurationException` at startup, naming the offending value, instead of quietly matching nothing:\n\n```typescript\nconsumer\n  .apply(LoggerMiddleware)\n  .forRoutes(SomeBaseClassWithoutController); \u002F\u002F now throws at startup\n```\n\nIt's a different exception class from `InvalidEnhancerException`, because middleware target resolution is a separate code path from enhancer resolution — but it's the same underlying fix philosophy in the same release: a configuration mistake that used to disappear into \"matches nothing\" now surfaces as a startup failure you can't miss.\n\n\u003C!-- playground:start -->\n\n## 🎮 Try it yourself\n\n**[▶️ Open the interactive playground →](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-invalid-enhancer-exception\u002Fplayground)**\n\n_Runs right in your browser — poke at it and watch the concept react live._\n\n\u003C!-- playground:end -->\n\n## Edge cases and gotchas\n\n- **Exception filters are the one enhancer kind this still doesn't cover.** A filter that can't run (missing `catch()`) only logs a warning — it doesn't throw. That's a real asymmetry in 12.5: don't assume every enhancer kind now fails loudly, and don't rely on a bad filter binding to crash your app the way a bad guard binding will.\n- **Request-scoped and transient enhancers get a pass in one specific case.** Nest builds lightweight placeholder instances for `REQUEST`- and `TRANSIENT`-scoped enhancers outside of an actual request context (for example, while wiring up the module graph). `assertEnhancer` skips those placeholders rather than failing on a stand-in that was never meant to be called directly — the real check still happens once a request resolves the real, per-request instance.\n- **A deliberately empty factory result is allowed.** If an `APP_GUARD`'s `useFactory` is written to conditionally return nothing (`() => (flag ? SomeGuard : undefined)`), that's treated as \"no enhancer here\" and skipped, not an error — this is a known pattern for feature-flagging a global guard, and 12.5 preserves it.\n- **This upgrade can turn a dormant bug into a boot-time crash.** If your app already has a misconfigured enhancer like the ones above, it has been silently running unguarded, unintercepted, or unvalidated routes in production. Upgrading to 12.5 won't introduce the bug — it will surface one that was already there. Upgrade in staging first, specifically so you find out there, not when production refuses to start.\n\n## Best practices\n\n- **Default to `useClass` for a guard, interceptor, or pipe bound through `APP_GUARD`\u002F`APP_INTERCEPTOR`\u002F`APP_PIPE`**, unless you specifically need `useFactory` for conditional construction — and if you do, make sure every branch returns either a real instance\u002Fclass or `undefined`, never the bare class reference by accident.\n- **Always call a guard\u002Finterceptor factory before binding it**: `@UseGuards(RolesGuard('admin'))`, never `@UseGuards(RolesGuard)`. If you rename a factory to take no arguments, double-check every call site still invokes it.\n- **Don't lean on an abstract class as a directly-bound enhancer token** unless a concrete implementation is actually registered against it somewhere in the module graph.\n- **Treat exception filters with extra care during review**, since a misbound filter still won't tell you at startup — it only warns, and warnings get lost in noisy logs.\n- **Upgrade to 12.5 in staging first**, and treat any new `InvalidEnhancerException` or `InvalidMiddlewareConfigurationException` at boot as a genuine, pre-existing bug report — not a false positive to silence.\n\n## FAQ\n\n### Will upgrading to NestJS 12.5 break my working app?\n\nOnly if an enhancer or a `forRoutes()`\u002F`exclude()` entry was already misconfigured in a way that made it silently do nothing. A correctly-bound guard, interceptor, or pipe behaves exactly as before — the new checks only fire on a shape that was always wrong.\n\n### What's the actual difference between `useValue` and `useClass` here?\n\n`useClass: SomeGuard` tells Nest to construct a `SomeGuard` instance through the DI container, resolving its constructor dependencies. `useValue: SomeGuard` tells Nest to use the exact value you passed — in this case the class itself, not an instance of it — with no construction step at all. For a guard, you almost always want the constructed instance, so `useClass` (or a correctly-called factory) is the right call.\n\n### Does this validation apply to exception filters too?\n\nNo. Per the 12.5 release notes, a guard, interceptor, or pipe that can't run now fails the application at startup; a filter that can't run still only logs a warning. Review filter bindings with extra attention until that changes.\n\n### Does a request-scoped guard get checked differently from a singleton one?\n\nThe check is the same `assertEnhancer` call either way — it just runs at a different point. A singleton (the default scope) is checked once, when it's constructed at bootstrap. A `REQUEST`- or `TRANSIENT`-scoped enhancer is checked each time a real instance is resolved for an actual request; placeholder instances built outside a request context are skipped so they don't trip the check on a stand-in.\n\n### Is this change itself a breaking change?\n\nNestJS ships it as a bug fix, not a new feature with an opt-out — a misconfigured enhancer silently doing nothing was never the intended behavior. But because it changes a previously-silent failure into a startup crash, treat upgrading like any fix that can change observable behavior: run it in staging and read every new boot-time exception before you deploy.\n\n## Cheat sheet\n\n| Mistake | What happens pre-12.5 | What happens on 12.5+ | Fix |\n| --- | --- | --- | --- |\n| `{ provide: APP_GUARD, useValue: AuthGuard }` | Guard silently dropped; routes run unguarded | `InvalidEnhancerException`: missing `\"canActivate\"` | `{ provide: APP_GUARD, useClass: AuthGuard }` |\n| `@UseGuards(RolesGuard)` where `RolesGuard` is an uncalled factory | Guard silently dropped | `InvalidEnhancerException` on the bound class | `@UseGuards(RolesGuard('admin'))` — call the factory |\n| An interceptor\u002Fpipe bound the same way (`useValue` with a class, or uncalled factory) | Silently dropped from the pipeline | `InvalidEnhancerException`: missing `\"intercept\"`\u002F`\"transform\"` | Use `useClass`, or call the factory |\n| `useFactory` that conditionally returns `undefined` | Skipped (intended, feature-flag pattern) | Still skipped — not an error | No change needed |\n| Exception filter that doesn't implement `catch()` | Silently dropped | **Still only a warning, no throw** | Fix the filter; don't rely on a crash to catch it |\n| `.forRoutes(SomeClassWithoutController)` | Middleware matches nothing, silently | `InvalidMiddlewareConfigurationException` | Add `@Controller()`, or pass a valid `{ path, method }` route object |\n\n```typescript\n\u002F\u002F The correct, DI-respecting way to bind every global enhancer\n@Module({\n  providers: [\n    AuthGuard,\n    LoggingInterceptor,\n    ValidationPipe,\n    { provide: APP_GUARD, useClass: AuthGuard },\n    { provide: APP_INTERCEPTOR, useClass: LoggingInterceptor },\n    { provide: APP_PIPE, useClass: ValidationPipe },\n  ],\n})\nexport class AppModule {}\n```\n\n\n\u003C!-- quiz:start -->\n\n## 🧠 Test yourself\n\nThink it clicked? **[Take the 8-question quiz →](https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-invalid-enhancer-exception\u002Fquiz)**\n\n_Instant feedback, a hint on every question, and an explanation for each answer — right or wrong._\n\n\u003C!-- quiz:end -->\n## Key takeaways\n\n- A guard, interceptor, or pipe is resolved through the same DI container as any provider, but then checked for a required method (`canActivate`, `intercept`, `transform`) that nothing at compile time enforces — that gap is where this bug lived.\n- Before NestJS 12.5, an enhancer with the wrong shape was silently dropped from the pipeline; the route it was bound to ran exactly as if the guard, interceptor, or pipe didn't exist.\n- `InvalidEnhancerException` (12.5+) throws at startup the moment a misconfigured guard, interceptor, or pipe is resolved — whether that's at `app.useGlobal*()`, a decorator, an `APP_*` provider, or per-request resolution for a scoped enhancer.\n- `useValue: SomeClass` is not the same as `useClass: SomeClass` — the first hands Nest the class itself with no instantiation, the second constructs a real instance through DI.\n- Exception filters are the one exception: a filter that can't run still only logs a warning. Upgrading to 12.5 won't fix that on its own, and it can turn a dormant guard\u002Finterceptor\u002Fpipe bug into a startup crash — upgrade in staging first.\n\nIf your `AuthGuard` typo makes it to production today, nothing will tell you — the route will just work for everyone, guarded or not. As of NestJS 12.5, that same typo stops your app before it ever opens a port. That's a straightforward trade: a crash you see in CI beats a security hole you find in an incident review.\n\nThis series has already covered the two pieces this bug sits between: [how Nest's DI container resolves providers and scopes](https:\u002F\u002Fdev.to\u002Fparsajiravand\u002Fnestjs-dependency-injection-explained-with-cheat-sheet-4bem) in the first place, and [how a guard's `canActivate` actually gets called](https:\u002F\u002Fdev.to\u002Fparsajiravand\u002Fnestjs-guards-canactivate-executioncontext-reflector-1onm) once it's resolved correctly. If you write tests against provider overrides, [the testing module episode](https:\u002F\u002Fdev.to\u002Fparsajiravand\u002Fnestjs-testing-module-provider-overrides-with-cheat-sheet-1o79) covers the DI-aware way to swap a guard for a fake in a test — the same `useClass`\u002F`useValue` distinction this article turns on.\n\nHas a provider binding typo ever shipped to production silently in one of your apps — and if so, what finally surfaced it: a log, a test, or a user?\n\n---\n\n🚀 **Want more like this?** Every guide, playground, and quiz lives on **[bestpractic.org](https:\u002F\u002Fbestpractic.org\u002F)** — open it and **[sign up free](https:\u002F\u002Fbestpractic.org\u002F)** so the next one finds you.\n\n*Thanks for reading! Let's stay connected:*\n\n- ⭐ **GitHub** — follow me and star the projects: [github.com\u002Fparsajiravand](https:\u002F\u002Fgithub.com\u002Fparsajiravand)\n- 💬 **Discord** — join the frontend best-practices community: [discord.gg\u002Fd9KRhuAwQ](https:\u002F\u002Fdiscord.gg\u002Fd9KRhuAwQ)\n- 📸 **Instagram** — frontend best practices, daily: [@bestpractice___](https:\u002F\u002Fwww.instagram.com\u002Fbestpractice___\u002F)",{"title":47,"canonical":404,"description":49},"https:\u002F\u002Fbestpractic.org\u002Fblog\u002Fnestjs-weekly-invalid-enhancer-exception","01a1fce0-e8b7-75e3-a22a-0390a8253c0f",{"name":407,"part":408,"total":408,"items":409},"NestJS Deep Dive",13,[410,414,419,423,427,431,435,439,443,448,452,456,461],{"slug":411,"title":412,"publishedAt":413,"readingMinutes":408},"nestjs-weekly-request-lifecycle","NestJS Request Lifecycle Explained (with Cheat Sheet)","2026-08-28T17:01:41.941Z",{"slug":415,"title":416,"publishedAt":417,"readingMinutes":418},"nestjs-weekly-dependency-injection-providers-scopes","NestJS Dependency Injection Explained (with Cheat Sheet)","2026-09-13T08:40:18.434Z",14,{"slug":420,"title":421,"publishedAt":422,"readingMinutes":51},"nestjs-weekly-module-encapsulation-exports","NestJS Module Encapsulation Explained (with Cheat Sheet)","2026-09-13T08:40:50.070Z",{"slug":424,"title":425,"publishedAt":426,"readingMinutes":408},"nestjs-weekly-testing-provider-overrides","NestJS Testing Module: Provider Overrides (with Cheat Sheet)","2026-09-18T17:23:03.842Z",{"slug":428,"title":429,"publishedAt":430,"readingMinutes":418},"nestjs-weekly-guards-canactivate-reflector","NestJS Guards: CanActivate, ExecutionContext & Reflector","2026-09-26T12:25:39.001Z",{"slug":432,"title":433,"publishedAt":434,"readingMinutes":418},"nestjs-weekly-standard-schema-validation","NestJS 12: Standard Schema Validation Without class-validator","2026-10-02T06:11:02.679Z",{"slug":436,"title":437,"publishedAt":438,"readingMinutes":408},"nestjs-weekly-lifecycle-hook-order","NestJS 12 Lifecycle Hooks: The Order You Can't Assume","2026-10-23T06:10:32.276Z",{"slug":440,"title":441,"publishedAt":442,"readingMinutes":51},"nestjs-weekly-optional-not-inherited","NestJS 12: @Optional() Not Inherited by a Redeclared Constructor","2026-10-30T05:36:46.066Z",{"slug":444,"title":445,"publishedAt":446,"readingMinutes":447},"nestjs-weekly-validation-error-target","NestJS 12.2: Why validationError.target Is Now undefined","2026-11-06T06:10:24.442Z",12,{"slug":449,"title":450,"publishedAt":451,"readingMinutes":51},"nestjs-weekly-app-filter-scope","NestJS APP_FILTER Scope: Why Your Global Filter Might Not Fire","2026-11-21T06:53:12.171Z",{"slug":453,"title":454,"publishedAt":455,"readingMinutes":418},"nestjs-weekly-platform-node-adapter","NestJS platform-node (12.3): The Adapter That Reorders Your Routes","2026-11-27T11:30:39.049Z",{"slug":457,"title":458,"publishedAt":459,"readingMinutes":460},"nestjs-weekly-interceptor-order","NestJS Interceptor Order: Why Stacking Breaks Your Cache","2026-11-27T11:44:57.150Z",16,{"slug":46,"title":47,"publishedAt":52,"readingMinutes":51},{"id":463,"locked":18},"01a1fce0-e8ec-75f8-88c9-ef45d5eb5406",[465],{"id":466,"slug":46,"title":467,"_count":468},"01a1fce0-e90a-7496-803f-e6bae8bb0931","NestJS InvalidEnhancerException: Enhancer Resolution and the 12.5 Fix",{"questions":469},8,[471],{"locale":13,"slug":46},{"id":466,"slug":46,"title":467,"_count":473,"questionCount":469},{"questions":469},[475,479,483,487,489,493,495,499,503,507,511,515],{"slug":476,"name":477,"articles":478},"webdev","Webdev",203,{"slug":480,"name":481,"articles":482},"javascript","Javascript",159,{"slug":484,"name":485,"articles":486},"frontend","Frontend",106,{"slug":72,"name":73,"articles":488},93,{"slug":490,"name":491,"articles":492},"css","Css",60,{"slug":66,"name":67,"articles":494},38,{"slug":496,"name":497,"articles":498},"performance","Performance",34,{"slug":500,"name":501,"articles":502},"programming","Programming",27,{"slug":504,"name":505,"articles":506},"react","React",23,{"slug":508,"name":509,"articles":510},"node","Node",19,{"slug":512,"name":513,"articles":514},"browser","Browser",18,{"slug":516,"name":517,"articles":418},"nextjs","Nextjs",[],{"items":520,"meta":623},[521,533,548,563,578,593,608],{"id":45,"slug":46,"title":47,"subtitle":48,"excerpt":49,"coverUrl":50,"locale":13,"readingMinutes":51,"publishedAt":52,"viewCount":522,"likeCount":19,"commentCount":19,"author":523,"vertical":524,"topic":525,"tags":526,"_count":531,"playground":532,"hasQuiz":17,"hasPlayground":17},71,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[527,528,529,530],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},{"slug":69,"name":70,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":46},{"id":534,"slug":457,"title":458,"subtitle":48,"excerpt":535,"coverUrl":536,"locale":13,"readingMinutes":460,"publishedAt":459,"viewCount":537,"likeCount":19,"commentCount":19,"author":538,"vertical":539,"topic":540,"tags":541,"_count":546,"playground":547,"hasQuiz":17,"hasPlayground":17},"01a1a1b0-49e9-711f-aea2-cbc2b73acc75","NestJS interceptor order decides whether a cache hit skips your response transform. Learn the onion model behind next.handle() — plus a copy-paste cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-interceptor-order.png",215,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[542,543,544,545],{"slug":61,"name":62,"color":48},{"slug":508,"name":509,"color":48},{"slug":66,"name":67,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":457},{"id":549,"slug":453,"title":454,"subtitle":48,"excerpt":550,"coverUrl":551,"locale":13,"readingMinutes":418,"publishedAt":455,"viewCount":552,"likeCount":19,"commentCount":19,"author":553,"vertical":554,"topic":555,"tags":556,"_count":561,"playground":562,"hasQuiz":17,"hasPlayground":17},"01a1c5e4-28dd-73bd-882b-dee70aa37883","NestJS 12.3 shipped platform-node, a built-in HTTP adapter that matches routes like Fastify, not Express. Learn the trap, plus a cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-platform-node-adapter.png",305,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[557,558,559,560],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},{"slug":508,"name":509,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":453},{"id":564,"slug":449,"title":450,"subtitle":48,"excerpt":565,"coverUrl":566,"locale":13,"readingMinutes":51,"publishedAt":451,"viewCount":567,"likeCount":19,"commentCount":19,"author":568,"vertical":569,"topic":570,"tags":571,"_count":576,"playground":577,"hasQuiz":17,"hasPlayground":17},"01a18121-99fa-77f0-83de-5cc8814cbbd4","A request-scoped APP_FILTER in NestJS can silently stop catching exceptions. Learn the fix NestJS 12 shipped, plus a copy-paste cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-app-filter-scope.png",347,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[572,573,574,575],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},{"slug":508,"name":509,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":449},{"id":579,"slug":444,"title":445,"subtitle":48,"excerpt":580,"coverUrl":581,"locale":13,"readingMinutes":447,"publishedAt":446,"viewCount":582,"likeCount":19,"commentCount":19,"author":583,"vertical":584,"topic":585,"tags":586,"_count":591,"playground":592,"hasQuiz":17,"hasPlayground":17},"01a15d15-305d-77da-a29a-d16a7df8ed3f","NestJS 12.2 defaults validationError.target to false, so a custom exceptionFactory reading error.target now gets undefined. The fix, plus a cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-validation-error-target.png",422,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[587,588,589,590],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},{"slug":508,"name":509,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":444},{"id":594,"slug":440,"title":441,"subtitle":48,"excerpt":595,"coverUrl":596,"locale":13,"readingMinutes":51,"publishedAt":442,"viewCount":597,"likeCount":19,"commentCount":19,"author":598,"vertical":599,"topic":600,"tags":601,"_count":606,"playground":607,"hasQuiz":17,"hasPlayground":17},"01a11150-cc6d-7657-8fe5-ae7578a8058b","NestJS 12 stops inheriting @Optional() once a subclass redeclares its own constructor — not when it has none at all. Cheat sheet included.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-optional-not-inherited.png",498,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[602,603,604,605],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},{"slug":508,"name":509,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":440},{"id":609,"slug":436,"title":437,"subtitle":48,"excerpt":610,"coverUrl":611,"locale":13,"readingMinutes":408,"publishedAt":438,"viewCount":612,"likeCount":19,"commentCount":19,"author":613,"vertical":614,"topic":615,"tags":616,"_count":621,"playground":622,"hasQuiz":17,"hasPlayground":17},"01a1358a-158e-70df-8178-64af7e9a5932","NestJS 12 changed how lifecycle hooks are ordered. Learn what onModuleInit and onApplicationBootstrap really guarantee, plus a copy-paste cheat sheet.","\u002Fmedia\u002Fcovers\u002Fnestjs-weekly-lifecycle-hook-order.png",513,{"id":55,"name":56,"username":57,"avatarUrl":48,"headline":58},{"slug":6,"name":7,"accentFrom":10,"accentTo":11},{"slug":61,"name":62},[617,618,619,620],{"slug":61,"name":62,"color":48},{"slug":66,"name":67,"color":48},{"slug":508,"name":509,"color":48},{"slug":72,"name":73,"color":48},{"assessments":75},{"slug":436},{"page":75,"perPage":624,"total":408,"totalPages":31},7,"\u003Cdiv class=\"shj shj-lang-ts shj-multiline\" data-lang=\"ts\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003Cdiv>7\u003C\u002Fdiv>\u003Cdiv>8\u003C\u002Fdiv>\u003Cdiv>9\u003C\u002Fdiv>\u003Cdiv>10\u003C\u002Fdiv>\u003Cdiv>11\u003C\u002Fdiv>\u003Cdiv>12\u003C\u002Fdiv>\u003Cdiv>13\u003C\u002Fdiv>\u003Cdiv>14\u003C\u002Fdiv>\u003Cdiv>15\u003C\u002Fdiv>\u003Cdiv>16\u003C\u002Fdiv>\u003Cdiv>17\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-cmnt\">\u002F\u002F auth.guard.ts\n\u003C\u002Fspan>@\u003Cspan class=\"shj-class\">Injectable\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n\u003Cspan class=\"shj-kwd\">export\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">class\u003C\u002Fspan> \u003Cspan class=\"shj-class\">AuthGuard\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">implements\u003C\u002Fspan> \u003Cspan class=\"shj-class\">CanActivate\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  \u003Cspan class=\"shj-func\">canActivate\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>context\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">ExecutionContext\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-type\">: boolean\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n    \u003Cspan class=\"shj-kwd\">const\u003C\u002Fspan> req \u003Cspan class=\"shj-oper\">=\u003C\u002Fspan> context\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">switchToHttp\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">getRequest\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n    \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> \u003Cspan class=\"shj-class\">Boolean\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>req\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>headers\u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>authorization\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>;\n  \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\n\n\u003Cspan class=\"shj-cmnt\">\u002F\u002F app.module.ts — the bug\n\u003C\u002Fspan>@\u003Cspan class=\"shj-class\">Module\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  providers\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">[\u003C\u002Fspan>\n    \u003Cspan class=\"shj-class\">AuthGuard\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n    \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> provide\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-num\">APP_GUARD\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> useValue\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">AuthGuard\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> \u003Cspan class=\"shj-cmnt\">\u002F\u002F should be useClass\n\u003C\u002Fspan>  \u003Cspan class=\"shj-bracket\">]\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n\u003Cspan class=\"shj-kwd\">export\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">class\u003C\u002Fspan> \u003Cspan class=\"shj-class\">AppModule\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-ts shj-multiline\" data-lang=\"ts\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003Cdiv>7\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">@\u003Cspan class=\"shj-class\">Module\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  providers\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">[\u003C\u002Fspan>\n    \u003Cspan class=\"shj-class\">AuthGuard\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n    \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> provide\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-num\">APP_GUARD\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> useClass\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">AuthGuard\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> \u003Cspan class=\"shj-cmnt\">\u002F\u002F instantiates AuthGuard via DI\n\u003C\u002Fspan>  \u003Cspan class=\"shj-bracket\">]\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n\u003Cspan class=\"shj-kwd\">export\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">class\u003C\u002Fspan> \u003Cspan class=\"shj-class\">AppModule\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-ts shj-multiline\" data-lang=\"ts\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003Cdiv>7\u003C\u002Fdiv>\u003Cdiv>8\u003C\u002Fdiv>\u003Cdiv>9\u003C\u002Fdiv>\u003Cdiv>10\u003C\u002Fdiv>\u003Cdiv>11\u003C\u002Fdiv>\u003Cdiv>12\u003C\u002Fdiv>\u003Cdiv>13\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-kwd\">function\u003C\u002Fspan> \u003Cspan class=\"shj-class\">RolesGuard\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">...\u003C\u002Fspan>roles\u003Cspan class=\"shj-type\">: string\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">[\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">]\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  @\u003Cspan class=\"shj-class\">Injectable\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n  \u003Cspan class=\"shj-kwd\">class\u003C\u002Fspan> \u003Cspan class=\"shj-class\">RolesGuardImpl\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">implements\u003C\u002Fspan> \u003Cspan class=\"shj-class\">CanActivate\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n    \u003Cspan class=\"shj-func\">canActivate\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>context\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">ExecutionContext\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-type\">: boolean\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n      \u003Cspan class=\"shj-cmnt\">\u002F\u002F ...check roles...\n\u003C\u002Fspan>      \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> \u003Cspan class=\"shj-bool\">true\u003C\u002Fspan>;\n    \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\n  \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\n  \u003Cspan class=\"shj-kwd\">return\u003C\u002Fspan> \u003Cspan class=\"shj-class\">RolesGuardImpl\u003C\u002Fspan>;\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\n\n@\u003Cspan class=\"shj-class\">UseGuards\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-class\">RolesGuard\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-str\">'admin'\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan> \u003Cspan class=\"shj-cmnt\">\u002F\u002F correct — the factory is called, returning a class\n\u003C\u002Fspan>@\u003Cspan class=\"shj-class\">UseGuards\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-class\">RolesGuard\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>          \u003Cspan class=\"shj-cmnt\">\u002F\u002F bug — this is the factory function itself\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-plain shj-multiline\" data-lang=\"plain\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">[Nest] ERROR [ExceptionHandler] InvalidEnhancerException: The guard\n(AuthGuard) doesn't provide the \u003Cspan class=\"shj-str\">\"canActivate\"\u003C\u002Fspan> method, so it can't run\nand the handlers it is bound to would run without it.\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-ts shj-multiline\" data-lang=\"ts\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">consumer\n  \u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">apply\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-class\">LoggerMiddleware\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n  \u003Cspan class=\"shj-oper\">.\u003C\u002Fspan>\u003Cspan class=\"shj-func\">forRoutes\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-class\">SomeBaseClassWithoutController\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>; \u003Cspan class=\"shj-cmnt\">\u002F\u002F now throws at startup\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>","\u003Cdiv class=\"shj shj-lang-ts shj-multiline\" data-lang=\"ts\">\u003Cdiv class=\"shj-scroll\">\u003Cdiv class=\"shj-numbers\">\u003Cdiv>1\u003C\u002Fdiv>\u003Cdiv>2\u003C\u002Fdiv>\u003Cdiv>3\u003C\u002Fdiv>\u003Cdiv>4\u003C\u002Fdiv>\u003Cdiv>5\u003C\u002Fdiv>\u003Cdiv>6\u003C\u002Fdiv>\u003Cdiv>7\u003C\u002Fdiv>\u003Cdiv>8\u003C\u002Fdiv>\u003Cdiv>9\u003C\u002Fdiv>\u003Cdiv>10\u003C\u002Fdiv>\u003Cdiv>11\u003C\u002Fdiv>\u003Cdiv>12\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"shj-code\">\u003Cspan class=\"shj-cmnt\">\u002F\u002F The correct, DI-respecting way to bind every global enhancer\n\u003C\u002Fspan>@\u003Cspan class=\"shj-class\">Module\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">(\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\n  providers\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">[\u003C\u002Fspan>\n    \u003Cspan class=\"shj-class\">AuthGuard\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n    \u003Cspan class=\"shj-class\">LoggingInterceptor\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n    \u003Cspan class=\"shj-class\">ValidationPipe\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n    \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> provide\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-num\">APP_GUARD\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> useClass\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">AuthGuard\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n    \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> provide\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-num\">APP_INTERCEPTOR\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> useClass\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">LoggingInterceptor\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n    \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan> provide\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-num\">APP_PIPE\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan> useClass\u003Cspan class=\"shj-oper\">:\u003C\u002Fspan> \u003Cspan class=\"shj-class\">ValidationPipe\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n  \u003Cspan class=\"shj-bracket\">]\u003C\u002Fspan>\u003Cspan class=\"shj-oper\">,\u003C\u002Fspan>\n\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">)\u003C\u002Fspan>\n\u003Cspan class=\"shj-kwd\">export\u003C\u002Fspan> \u003Cspan class=\"shj-kwd\">class\u003C\u002Fspan> \u003Cspan class=\"shj-class\">AppModule\u003C\u002Fspan> \u003Cspan class=\"shj-bracket\">{\u003C\u002Fspan>\u003Cspan class=\"shj-bracket\">}\u003C\u002Fspan>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>",{"locked":18,"total":19,"comments":632},[]]