Binh Ho
Binh Ho

Reputation: 4993

NestJS swagger-ui-express package is missing after deploy

I am using NestJS version 7.1.0 and serverless and creating api document uses nestjs swagger. In local it's worked like champ but after deploy got error.

swagger-ui-express package is missing

I see some issue here but still not resolve this issue. https://github.com/nestjs/swagger/issues/277

This is my package.json

"dependencies": {
    "@nestjs/common": "^7.1.0",
    "@nestjs/core": "^7.1.0",
    "@nestjs/passport": "^7.0.0",
    "@nestjs/platform-express": "^7.0.7",
    "@nestjs/swagger": "^4.5.8",
    "aws-serverless-express-binary": "^1.0.1",
    "class-transformer": "^0.2.3",
    "class-validator": "^0.11.1",
    "dayjs": "^1.8.24",
    "dotenv": "^8.2.0",
    "dynamodb-data-types": "^3.0.1",
    "express": "^4.17.1",
    "faker": "^4.1.0",
    "jest": "^24.9.0",
    "jwks-rsa": "^1.6.0",
    "latinize": "^0.4.1",
    "lodash": "^4.17.15",
    "mime-types": "^2.1.26",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.0",
    "rxjs": "^6.5.3",
    "serverless-dynamodb-local": "^0.2.39",
    "swagger-ui-express": "^4.1.4",
    "tslint-loader": "^3.5.4",
    "uuid": "^3.3.3",
    "winston": "^3.2.1"
  },

This is the config:

const options = new DocumentBuilder()
  .setTitle("My APIs")
  .setDescription("APIs description")
  .setVersion("1.0")
  .addTag("cats")
  .build();

const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup("api", app, document);

Any help!

Upvotes: 3

Views: 6210

Answers (4)

Sergey Pogranichniy
Sergey Pogranichniy

Reputation: 466

My solution was to install express:

npm i express

Upvotes: 15

Oluwafisayo Owolo
Oluwafisayo Owolo

Reputation: 49

I had to run:

yarn install

to fix it.

Upvotes: 0

Binh Ho
Binh Ho

Reputation: 4993

I figured out the solutions, hope this help someone. I add a fake endpoint to make sure swagger-ui-express not be removed after webpack ran.

if (event.path === "/api") {
    event.path = "/api/";
} else if (event.path.includes("/swagger-ui")) {
    event.path = event.path.replace("/swagger-ui", "/api/swagger-ui");
}

// Fake to keep swagger-ui-express work after deploy
if (event.path === "/fake-swagger-ui-express") {
    swaggerUi.setup(null);
}

Upvotes: 0

lamlam
lamlam

Reputation: 1

Do you happen to be using yarn workspaces? I had the same problem just now, where locally everything worked but broke on our cluster.

If yes, then try adding this to the package.json of your API that is using the swagger module: "workspaces": { "nohoist": ["swagger-ui-express"] }

Upvotes: -1

Related Questions