swagger-express-mw is an abandoned CommonJS middleware for integrating Swagger (OpenAPI 2.0) definitions with Express applications. It functions primarily as a wrapper around the `swagger-node-runner` package, handling the loading and application of Swagger definitions to an Express server. The package's current stable version is 0.7.0, released in January 2017. Both `swagger-express-mw` and its core dependency `swagger-node-runner` have not seen active development or releases in over seven years, indicating a lack of maintenance. While it simplifies the setup of Swagger-driven APIs by automating routing and validation based on a YAML or JSON definition, its age means it only supports Swagger 2.0 and is not compatible with modern OpenAPI 3.x specifications. There are several forks like `swagger-express-mw-fork` and `swagger-node-runner-fixed` attempting to address some of the issues or update dependencies for newer Node.js versions.
npm install swagger-express-mwVerified import paths — ran on the pinned version, not inferred.
This quickstart initializes an Express application with `swagger-express-mw`, dynamically creating a basic Swagger 2.0 YAML definition and registering the middleware. It demonstrates how to set up a simple API endpoint defined in Swagger and exposes the Swagger definition.
Consult the `swagger-node-runner@0.6.0` release notes for detailed upgrade instructions, including potential changes to configuration and controller implementations.
For new projects or existing projects requiring OpenAPI 3.x support, consider modern alternatives like `express-openapi`, `swagger-express-middleware`, or `nestjs/swagger`. For critical security updates or Node.js compatibility, evaluate community-maintained forks like `swagger-node-runner-fixed` or `swagger-express-mw-fork`, but proceed with caution due to their limited maintenance.
Update vulnerable transitive dependencies using `npm audit fix --force` or `yarn resolutions` if possible, but be aware that this may lead to breakage given the unmaintained nature of the primary package. The most robust fix is to migrate to a actively maintained OpenAPI middleware solution.
Ensure your Swagger definition file is correctly named (`swagger.yaml` or `swagger.json`) and placed in `your_app_root/api/swagger/`. Alternatively, specify the `swaggerFile` property in the configuration object passed to `swaggerExpressMw.create()` with the absolute path to your definition file.
Check the `err` callback parameter in `swaggerExpressMw.create()` for any errors during initialization (e.g., malformed Swagger file, missing dependencies). Ensure all configuration options are valid and that `appRoot` points to a valid directory.
Carefully review your `swagger.yaml` file for proper YAML syntax, paying close attention to indentation. Use a YAML linter or editor with YAML validation to identify and correct errors.