Swagger UI Dist

5.32.4 · active · verified Wed Apr 22

swagger-ui-dist is an npm package that provides the compiled, production-ready 'dist' folder of Swagger UI as a direct, dependency-free module. It is designed for scenarios where users prefer to serve Swagger UI's static assets directly, without the 'swagger-ui' package installing its own dependencies. The current stable version is 5.32.4, with frequent patch releases addressing bug fixes and security updates, alongside regular minor releases introducing features like OpenAPI 3.2.0 support and dark mode. Its primary differentiator is offering a pre-built bundle, making it ideal for integration into existing web servers (e.g., Express, Koa) or static file serving environments, offering a lean alternative to the more feature-rich but dependency-laden 'swagger-ui' package.

Common errors

Warnings

Install

Imports

Quickstart

Sets up an Express.js server to serve Swagger UI from its 'dist' folder, providing an API documentation endpoint.

const express = require('express');
const path = require('path');
const swaggerUiDist = require('swagger-ui-dist');

const app = express();
const port = 3000;

// Get the absolute file system path to the swagger-ui-dist directory
const swaggerUiAssetPath = swaggerUiDist.getAbsoluteFSPath();

// Serve the static Swagger UI files from the node_modules directory
app.use('/swagger-ui', express.static(swaggerUiAssetPath));

// Endpoint to display Swagger UI
app.get('/api-docs', (req, res) => {
  const swaggerSpecUrl = '/swagger.json'; // URL to your OpenAPI spec

  res.send(`
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Swagger UI</title>
        <link rel="stylesheet" type="text/css" href="/swagger-ui/swagger-ui.css" />
        <link rel="icon" type="image/png" href="/swagger-ui/favicon-32x32.png" sizes="32x32" />
        <link rel="icon" type="image/png" href="/swagger-ui/favicon-16x16.png" sizes="16x16" />
        <style>
            html { box-sizing: border-box; overflow: -moz-scrollbars-vertical; overflow-y: scroll; }
            *, *:before, *:after { box-sizing: inherit; }
            body { margin:0; background: #fafafa; }
        </style>
    </head>
    <body>
        <div id="swagger-ui"></div>
        <script src="/swagger-ui/swagger-ui-bundle.js" charset="UTF-8"> </script>
        <script src="/swagger-ui/swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
        <script>
            window.onload = function() {
                const ui = SwaggerUIBundle({
                    url: "${swaggerSpecUrl}",
                    dom_id: '#swagger-ui',
                    deepLinking: true,
                    presets: [
                        SwaggerUIBundle.presets.apis,
                        SwaggerUIStandalonePreset
                    ],
                    plugins: [
                        SwaggerUIBundle.plugins.DownloadUrl
                    ],
                    layout: "StandaloneLayout"
                });
                window.ui = ui;
            };
        </script>
    </body>
    </html>
  `);
});

// Example API spec endpoint
app.get('/swagger.json', (req, res) => {
  res.json({
    openapi: '3.0.0',
    info: {
      title: 'Example API',
      version: '1.0.0',
      description: 'A simple example API served with Swagger UI.',
    },
    paths: {
      '/hello': {
        get: {
          summary: 'Returns a greeting',
          responses: {
            '200': {
              description: 'Successful response',
              content: {
                'application/json': {
                  schema: {
                    type: 'object',
                    properties: {
                      message: {
                        type: 'string',
                        example: 'Hello from Example API!',
                      },
                    },
                  },
                },
              },
            },
          },
        },
      },
    },
  });
});

app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
  console.log(`Swagger UI available at http://localhost:${port}/api-docs`);
});

view raw JSON →