create-nestjs-middleware-module is a lightweight utility library designed to simplify the integration and configuration of Express or Fastify middleware into NestJS applications. It abstracts away the boilerplate of implementing `NestModule` and `MiddlewareConsumer` by providing a `createModule` function, allowing developers to define middleware factories that can accept options. The library currently supports NestJS versions 8 through 11 and requires Node.js >=18.0.0. It aims to provide an idiomatic NestJS way to encapsulate middleware logic within a modular structure, enabling configuration via `forRoot` or `forRootAsync` methods, and offering fine-grained control over routing, similar to NestJS's built-in `MiddlewareConfigProxy`. Its current stable version is 0.4.0, with releases occurring as needed to maintain compatibility with new NestJS versions.
npm install create-nestjs-middleware-moduleVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a functional middleware with options, convert it into a configurable NestJS module using `createModule`, and then integrate it into a `AppModule` with both explicit options and default usage.
Upgrade your NestJS version to >=8.0.0 and your Node.js version to >=16.0.0. Consider updating your `package.json` `engines` and `@nestjs/common` peer dependency.
Apply the `as FacadeModuleStaticOptional<YourOptionsInterface>` type assertion to the module created by `createModule` if you intend to allow `forRoot()` without any arguments.
Test your custom middleware on the target platform (Express or Fastify). You might need to conditionally implement logic or create separate middleware factories for different platforms if incompatibilities arise.
Review your application's middleware order, especially if you have global middleware defined both natively and via `create-nestjs-middleware-module` in NestJS v11.
Ensure the module is correctly imported and that `createModule` has been called with the middleware factory. Double-check the import path `from 'create-nestjs-middleware-module'` and verify the export name of your custom module. If the error occurs when calling `forRoot()` without arguments, ensure `FacadeModuleStaticOptional` type assertion is used.
Ensure any services or providers required by your middleware factory are either provided directly within the `createModule`'s configuration (if supported by a future API), made available globally, or refactor your middleware to receive only configuration options directly.
Ensure your middleware factory function correctly accepts an `options` parameter of type `T` (or `void` if no options are expected) and returns either a single middleware function `(req, res, next) => void` or an array of such functions. Verify the `T` generic type argument passed to `createModule` matches your `Options` interface.