nestjs/nest · critical · InvalidMiddlewareException
The middleware doesn't provide the 'use' method (${name})
Error message
The middleware doesn't provide the 'use' method (${name}) What it means
Middleware in NestJS must implement the NestMiddleware contract — an instance method `use(req, res, next)`. During middleware registration the framework checks `instance.use`; when it is undefined, InvalidMiddlewareException ('The middleware doesn't provide the use method (X)') stops bootstrap because the middleware could never handle a request.
Source
Thrown at packages/core/middleware/middleware-module.ts:254
applicationRef,
routeInfo,
moduleRef,
collection,
);
}
}
private async bindHandler(
wrapper: InstanceWrapper<NestMiddleware>,
applicationRef: HttpServer,
routeInfo: RouteInfo,
moduleRef: Module,
collection: Map<InjectionToken, InstanceWrapper>,
) {
const { instance, metatype } = wrapper;
if (isUndefined(instance?.use)) {
throw new InvalidMiddlewareException(metatype!.name);
}
const isStatic = wrapper.isDependencyTreeStatic();
if (isStatic) {
const proxy = await this.createProxy(instance);
return this.registerHandler(applicationRef, routeInfo, proxy);
}
const isTreeDurable = wrapper.isDependencyTreeDurable();
await this.registerHandler(
applicationRef,
routeInfo,
async <TRequest, TResponse>(
req: TRequest,
res: TResponse,
next: () => void,
) => {
try {View on GitHub (pinned to dd75d7bd8c)
Solutions
- Implement the interface: `export class LoggerMiddleware implements NestMiddleware { use(req: Request, res: Response, next: NextFunction) { ... } }`.
- Check for typos/renames and make sure `use` is a regular instance method (not static, not a getter).
- For existing Express-style functions, adapt them: `use(req, res, next) { expressFn(req, res, next); }` or use `consumer.apply(wrap(expressFn))` patterns.
- Add `implements NestMiddleware` so the compiler enforces the method before runtime.
Example fix
// before
export class AuthMiddleware { // no use()
handle(req: any, res: any, next: () => void) { next(); }
}
// after
import { NestMiddleware } from '@nestjs/common';
export class AuthMiddleware implements NestMiddleware {
use(req: Request, res: Response, next: NextFunction) {
// ...
next();
}
} Defensive patterns
Strategy: type-guard
Validate before calling
// Fail fast if a middleware class lacks use() before it reaches the framework
interface MiddlewareLike { use(...args: any[]): any }
const implementsUse = (cls: Function): cls is new () => MiddlewareLike =>
typeof (cls.prototype as any)?.use === 'function';
// in the module's configure():
if (!implementsUse(AuthMiddleware)) {
throw new Error('AuthMiddleware must implement NestMiddleware (use method)');
}
consumer.apply(AuthMiddleware).forRoutes('*'); Type guard
const isNestMiddleware = (v: any): v is import('@nestjs/common').NestMiddleware =>
!!v && typeof v.use === 'function'; Prevention
- Always write `implements NestMiddleware` on middleware classes so the compiler enforces use().
- Add a wiring test that iterates middleware classes and asserts typeof cls.prototype.use === 'function'.
- Wrap legacy Express functions in a class adapter instead of passing them to consumer.apply().
When it happens
Trigger: A class listed in `module.configure(consumer)` via `consumer.apply(X).forRoutes(...)` has no `use` method; `use` was renamed (handle/run), made static, arrow-bound as a class field on a subclass that lost it, or defined on an interface never implemented; applying a plain function or object instead of a class; TypeScript weakening the class to `any` so the missing method compiles.
Common situations: Porting Express middleware objects into NestJS without wrapping them in a class; renaming use() to match custom conventions; middleware split into base + derived class where the base declares `use` abstract but the derived forgets it; class-based middleware written by developers used to function middleware.
Related errors
- Conflicting HTTP routes detected: - ${messages} Adjust rou
- Calling the "${methodName}" in the preview mode is not suppo
- An invalid controller has been detected. "${className}" does
- Nest cannot create the ${parentModule.name} instance. The mo
- Nest cannot create the ${parentModule.name} instance. Receiv
AI-assisted analysis of nestjs/nest@dd75d7bd8c (2026-08-21).
Data as JSON: /api/errors/9a00bfbe01461065.
Report an issue: GitHub.