NestJS/Swagger错误TS 2307:找不到模块'@nestjs/swagger'

fxnxkyjh  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(460)

我尝试在我的NestJS应用程序中添加Swagger,但我一直收到一个错误,它找不到模块。我已经使用npm install --save @nestjs/swagger swagger-ui-express将其添加到package.json文件中。请注意,我也使用了带有docker-compose的docker。
主.ts文件:

import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const whitelistOrigins = process.env.ALLOWED_CORS_ORIGIN;
  app.enableCors({
    origin: (origin, callback) => {
      if (whitelistOrigins.indexOf(origin) !== -1) {
        callback(null, true);
      } else {
        callback(null, false);
      }
    },
    optionsSuccessStatus: 200,
  });

  const options = new DocumentBuilder()
    .setTitle('App')
    .setDescription('The App API;')
    .setVersion('1.0')
    .addTag('app')
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api', app, document);

  await app.listen(process.env.PORT);
}
bootstrap();

package.json文件:

"dependencies": {
    "@nestjs/common": "^6.3.1",
    "@nestjs/core": "^6.3.1",
    "@nestjs/jwt": "^6.1.1",
    "@nestjs/mongoose": "^6.1.2",
    "@nestjs/passport": "^6.1.0",
    "@nestjs/platform-express": "^6.3.2",
    "@nestjs/platform-socket.io": "^6.3.2",
    "@nestjs/swagger": "^3.1.0",
    "@nestjs/websockets": "^6.3.1",
    "bcrypt": "^3.0.6",
    "cache-manager": "^2.9.1",
    "cache-manager-redis-store": "^1.5.0",
    "dotenv": "^8.0.0",
    "google-auth-library": "^4.2.2",
    "helmet": "^3.21.1",
    "joi": "^14.3.1",
    "lodash": "^4.17.11",
    "moment": "^2.24.0",
    "mongoose": "^5.6.0",
    "nest-access-control": "^2.0.1",
    "nest-schedule": "^0.6.0",
    "passport": "^0.4.0",
    "passport-http-bearer": "^1.0.1",
    "passport-jwt": "^4.0.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^2.6.3",
    "rxjs": "^6.5.2",
    "socket.io-redis": "^5.2.0",
    "swagger-ui-express": "^4.1.2",
    "typescript": "^3.5.2"
  },
  "devDependencies": {
    "@nestjs/testing": "^6.3.1",
    "@types/express": "^4.17.0",
    "@types/jest": "^24.0.15",
    "@types/node": "^12.0.8",
    "@types/supertest": "^2.0.7",
    "jest": "^24.8.0",
    "nodemon": "^1.19.1",
    "prettier": "^1.18.2",
    "supertest": "^4.0.2",
    "ts-jest": "^24.0.2",
    "ts-loader": "^6.0.3",
    "ts-node": "^8.3.0",
    "tsconfig-paths": "^3.8.0",
    "tslint": "5.17.0"
  },

tsconfig.json文件:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "moduleResolution": "node"
  },
}

我在使用npm run start:devnpm run start:prod运行应用程序时不断收到以下错误:

/home/node/app/node_modules/ts-node/src/index.ts:293
     return new TSError(diagnosticText, diagnosticCodes)
            ^
 TSError: ⨯ Unable to compile TypeScript:
 src/main.ts(4,48): error TS2307: Cannot find module '@nestjs/swagger'.

     at createTSError (/home/node/app/node_modules/ts-node/src/index.ts:293:12)
     at reportTSError (/home/node/app/node_modules/ts-node/src/index.ts:297:19)
     at getOutput (/home/node/app/node_modules/ts-node/src/index.ts:399:34)
     at Object.compile (/home/node/app/node_modules/ts-node/src/index.ts:457:32)
     at Module.m._compile (/home/node/app/node_modules/ts-node/src/index.ts:530:43)
     at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
     at Object.require.extensions.(anonymous function) [as .ts] (/home/node/app/node_modules/ts-node/src/index.ts:533:12)
     at Module.load (internal/modules/cjs/loader.js:653:32)
     at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
     at Function.Module._load (internal/modules/cjs/loader.js:585:3)
 [nodemon] app crashed - waiting for file changes before starting...

docker-compose.yml文件:

version: "2"
services:
  node:
    build: 
      context: .
      dockerfile: ./docker/node/Dockerfile
    user: "node"
    working_dir: /home/node/app
    environment:
      - NODE_ENV=development
    volumes:
      - ./:/home/node/app
      - /home/node/app/node_modules/
    expose:
      - "8081"
    command: "npm run start:dev"
    ports:
      - "8000:8000"
    depends_on:
      - mongo
      - redis
  mongo:
    image: mongo
    ports:
      - "27018:27017"
    volumes:
      - data-volume:/data/db
  redis:
    image: redis
    ports:
      - "6379:6379"
  mongoclient:
    image: mongoclient/mongoclient:2.2.0
    depends_on:
      - mongo
    ports:
      - 3300:3000
volumes:
  data-volume:

/停靠程序/节点/停靠文件:

FROM node:10

WORKDIR /home/node/app
COPY ./package.json ./

RUN npm install nodemon -g
RUN npm install

在使用nodemon时收到此错误。

l3zydbqr

l3zydbqr1#

我成功地安装了@nestjs/swagger,但是我得到了这个错误:“错误TS 2307:找不到模块“@nestjs/swagger”或其相应的类型声明。”
我又加了一句“@nestjs/霸气”:“^5.2.1”并运行yarn。我解决了问题

z31licg0

z31licg02#

有时候,docker的构建会很奇怪,不能正确地构建。请重新构建你的main,然后再试一次。我正在使用NestJS框架(expressJS),遇到了一个类似的问题,但通过这种方法得到了解决。其他模块也遇到了同样的声明问题,也通过这种方法得到了解决。使用Swagger ^6.0.5。

相关问题