swagger-ui-express: Uncaught ReferenceError: SwaggerUIBundle is not defined at window.onload with SwaggerUI on typescript

I am trying to create documentation using swagger-ui-express and swagger-jsdoc as follows:

import swaggerJsDoc from 'swagger-jsdoc';
import swaggerUi from 'swagger-ui-express';

const options: swaggerJsDoc.Options={
    definition:
    {
        openapi: '3.0.0',
        info: {
            title: 'API',   
            version: '1.0.0',
            description: 'API',
        },
        servers: [ {
                url: 'http://localhost:3000},],},
    apis: ['./src/api/**/*.ts'],
  }

const specs = swaggerJsDoc(options);

 app.use('/api-docs',swaggerUi.serve,swaggerUi.setup(specs));
    app.get('/api-docs.json',(req:Request,res:Response)=>{
        res.setHeader('Content-Type','application/json');
        res.send(specs);
    }
    )

While running I get the following issue on console:

Uncaught SyntaxError: Unexpected token '<' (at swagger-ui-bundle.js:3:1)
swagger-ui-standalone-preset.js:3 
Uncaught SyntaxError: Unexpected token '<' (at swagger-ui-standalone-preset.js:3:1)
Uncaught ReferenceError: SwaggerUIBundle is not defined
    at window.onload

Is this issue related to any missing dependency or is this a bug?

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 6
  • Comments: 16

Most upvoted comments

While running I get the following issue on console:

Uncaught SyntaxError: Unexpected token '<' (at swagger-ui-bundle.js:3:1)
swagger-ui-standalone-preset.js:3 
Uncaught SyntaxError: Unexpected token '<' (at swagger-ui-standalone-preset.js:3:1)
Uncaught ReferenceError: SwaggerUIBundle is not defined
    at window.onload

I did not have this issue when running project locally, but when I deployed it to vercel - I simply had a blank screen, no resources were loaded where swagger ui should be:

image

But I found a workaround that has fixed my issue, had to edit vercel config:

image

Simply told vercel to take the needed swagger files from node_modules and then it works:

image

I faced the same problem while trying to build the code with esbuild and could solve it by adding swagger-ui-express as external using the flag: --external:swagger-ui-express.

Here is the complete build command of my project:

esbuild src/index.ts --bundle --platform=node --outfile=dist/index.js --external:express --external:cors --external:swagger-ui-express

I’m facing the same issue. also getting http 502 error for bundle js call ‘…/swagger-ui-bundle.js’. Couple of times the UI loaded for me but then after that was always getting the above error.

I think this has something to do with the configurations. Because the issue doesn’t appear for me in local and one of the environment , but only in my production env where certain things like load balancer etc are implemented.