Swagger: Is there a easy way to disable swagger in production mode but enable in development mode?

Created on 14 Jan 2019  Â·  5Comments  Â·  Source: nestjs/swagger

What I do now is not to call the SwaggerModule.setup() in production mode.

Most helpful comment

I have this:

 if (runEnv === RunEnvironment.Development) {
    const sw = await import('@nestjs/swagger')
    const swaggerOptions = new sw.DocumentBuilder()
      .setTitle('Sample API')
      .setVersion('0.1')
      .build()
    const document = sw.SwaggerModule.createDocument(app, swaggerOptions)
    sw.SwaggerModule.setup('docs', app, document)
  }

but, it still gets bundled into main.js during ng build api --prod and expects "swagger-ui-express" and "@nestjs/swagger" npm modules to be present in the production docker image (i.e. additional ~5.5MB). Would be nice if there was a way to get rid of it completely in prod environment.

All 5 comments

Add production env check and don't init SwaggerModule at all.

@Wizp
How about the swagger annotation in controller and dto?

@arfaWong they shouldn't affect anything at all.

I have this:

 if (runEnv === RunEnvironment.Development) {
    const sw = await import('@nestjs/swagger')
    const swaggerOptions = new sw.DocumentBuilder()
      .setTitle('Sample API')
      .setVersion('0.1')
      .build()
    const document = sw.SwaggerModule.createDocument(app, swaggerOptions)
    sw.SwaggerModule.setup('docs', app, document)
  }

but, it still gets bundled into main.js during ng build api --prod and expects "swagger-ui-express" and "@nestjs/swagger" npm modules to be present in the production docker image (i.e. additional ~5.5MB). Would be nice if there was a way to get rid of it completely in prod environment.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

otroboe picture otroboe  Â·  3Comments

itslenny picture itslenny  Â·  3Comments

ericzon picture ericzon  Â·  4Comments

dennisameling picture dennisameling  Â·  4Comments

leefordjudes picture leefordjudes  Â·  4Comments