← blog
NestjsDecember 4, 2026 · 15 min read

NestJS 12.5 InvalidEnhancerException: Why Guards Fail at Startup

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.

Parsa Jiravand · Frontend engineer · building bestpractic
NestJS 12.5 InvalidEnhancerException: Why Guards Fail at Startup

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.

This 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.

By the end of this article you'll be able to:

  • 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 useValue/useClass mistake that made a guard silently vanish from the pipeline before NestJS 12.5
  • Read an InvalidEnhancerException message and fix the binding it'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'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't expecting

You've bound at least one guard or interceptor with @UseGuards(), @UseInterceptors(), or the APP_GUARD/APP_INTERCEPTOR/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.

This is written against NestJS 12.x, verified against @nestjs/core'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 and NestJS's own merged fix commits — never from memory.

Here's the setup, reduced to the smallest form that reproduces it:

TypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// auth.guard.ts @Injectable() export class AuthGuard implements CanActivate { canActivate(context: ExecutionContext): boolean { const req = context.switchToHttp().getRequest(); return Boolean(req.headers.authorization); } } // app.module.ts — the bug @Module({ providers: [ AuthGuard, { provide: APP_GUARD, useValue: AuthGuard }, // should be useClass ], }) export class AppModule {}

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.

Before 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.

Nest 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.

When 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.

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.

This 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.

The fix for the opening example is one word:

TypeScript
1
2
3
4
5
6
7
@Module({ providers: [ AuthGuard, { provide: APP_GUARD, useClass: AuthGuard }, // instantiates AuthGuard via DI ], }) export class AppModule {}

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.

The 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:

An uncalled guard factory. A common pattern builds a guard dynamically, parameterized by roles:

TypeScript
1
2
3
4
5
6
7
8
9
10
11
12
13
function RolesGuard(...roles: string[]) { @Injectable() class RolesGuardImpl implements CanActivate { canActivate(context: ExecutionContext): boolean { // ...check roles... return true; } } return RolesGuardImpl; } @UseGuards(RolesGuard('admin')) // correct — the factory is called, returning a class @UseGuards(RolesGuard) // bug — this is the factory function itself

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.

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.

All 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.

Upgrade the app above to @nestjs/core@12.5.1 without fixing the binding, and instead of a clean boot, you get this at startup:

Text
1
2
3
[Nest] ERROR [ExceptionHandler] InvalidEnhancerException: The guard (AuthGuard) doesn't provide the "canActivate" method, so it can't run and the handlers it is bound to would run without it.

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.

The 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:

  • app.useGlobalGuards() / useGlobalInterceptors() / useGlobalPipes() — each argument is checked the moment you pass it in.
  • @UseGuards() / @UseInterceptors() / @UseBindPipes()-style decorators at controller or method scope — checked once Nest has constructed the bound class.
  • 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.
  • APP_GUARD / APP_INTERCEPTOR / 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.

The 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:

TypeScript
1
2
3
consumer .apply(LoggerMiddleware) .forRoutes(SomeBaseClassWithoutController); // now throws at startup

It'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.

Runs right in your browser — poke at it and watch the concept react live.

  • 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.
  • 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.
  • 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.
  • 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.

  • Default to useClass for a guard, interceptor, or pipe bound through APP_GUARD/APP_INTERCEPTOR/APP_PIPE, unless you specifically need useFactory for conditional construction — and if you do, make sure every branch returns either a real instance/class or undefined, never the bare class reference by accident.
  • Always call a guard/interceptor 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.
  • 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.
  • 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.
  • 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.

Only if an enhancer or a forRoutes()/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.

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.

No. 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.

The 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.

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.

MistakeWhat happens pre-12.5What happens on 12.5+Fix
{ provide: APP_GUARD, useValue: AuthGuard }Guard silently dropped; routes run unguardedInvalidEnhancerException: missing "canActivate"{ provide: APP_GUARD, useClass: AuthGuard }
@UseGuards(RolesGuard) where RolesGuard is an uncalled factoryGuard silently droppedInvalidEnhancerException on the bound class@UseGuards(RolesGuard('admin')) — call the factory
An interceptor/pipe bound the same way (useValue with a class, or uncalled factory)Silently dropped from the pipelineInvalidEnhancerException: missing "intercept"/"transform"Use useClass, or call the factory
useFactory that conditionally returns undefinedSkipped (intended, feature-flag pattern)Still skipped — not an errorNo change needed
Exception filter that doesn't implement catch()Silently droppedStill only a warning, no throwFix the filter; don't rely on a crash to catch it
.forRoutes(SomeClassWithoutController)Middleware matches nothing, silentlyInvalidMiddlewareConfigurationExceptionAdd @Controller(), or pass a valid { path, method } route object
TypeScript
1
2
3
4
5
6
7
8
9
10
11
12
// The correct, DI-respecting way to bind every global enhancer @Module({ providers: [ AuthGuard, LoggingInterceptor, ValidationPipe, { provide: APP_GUARD, useClass: AuthGuard }, { provide: APP_INTERCEPTOR, useClass: LoggingInterceptor }, { provide: APP_PIPE, useClass: ValidationPipe }, ], }) export class AppModule {}

Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.

  • 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.
  • 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.
  • 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.
  • 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.
  • 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/interceptor/pipe bug into a startup crash — upgrade in staging first.

If 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.

This series has already covered the two pieces this bug sits between: how Nest's DI container resolves providers and scopes in the first place, and how a guard's canActivate actually gets called once it's resolved correctly. If you write tests against provider overrides, the testing module episode covers the DI-aware way to swap a guard for a fake in a test — the same useClass/useValue distinction this article turns on.

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?


🚀 Want more like this? Every guide, playground, and quiz lives on bestpractic.org — open it and sign up free so the next one finds you.

Thanks for reading! Let's stay connected:

Keep reading

One post a day, in your inbox

Each one with a runnable playground and a quiz. No pitch, no digest, unsubscribe in one click.

0 comments

Sign in to join the discussion, like comments, and save articles for later.