Search code examples
node.jsswaggerswagger-3.0

Swagger 3.0.1 server generator


When I generate a nodejs-server with swagger 3.0.1 in the online tool https://editor.swagger.io/ and try to npm start the project I always get the following error:

enter image description here

Error: Cannot find module './middleware/swagger.router'


Solution

  • Short-term fix: you can get it to run until the maintainer of oas3-tools and Smartbear (for this server generator) fixes their respective parts.

    PROBLEM A: oas3-tools build script isn't copying the 'middleware' over to the 'dist' directory.

    To fix this:

    1. Start your generated server (this will run npm install and create the dist folder in node_modules/oas3-tools)
    2. Go into ./node_modules/oas3-tools and manually copy the missing src/middleware to the dist folder.
    3. Now that you have a middleware folder in your dist folder, run the following typescript compile command against it: tsc dist/middleware/

    PROBLEM B:

    The next issue is a typo on the path to the swagger document is incorrect.

    To fix this:

    1. In the index.js file of your generated server, change the string 'api/openapi.yaml' to 'api/swagger.yaml'

    POSSIBLE EXTRA PROBLEM:

    You may need to manually transpile your .ts files. Please see @Alex's comment below if you also run into this (i.e. error TS6053: File 'dist/middleware/.ts' not found. Found 1 error)