【问题标题】:NestJS / Swagger - Can't Import the ModuleNestJS / Swagger - 无法导入模块
【发布时间】:2021-09-18 05:53:07
【问题描述】:

我正在创建一个样板的 Nestjs 应用程序。我想将@nestjs/swagger 添加到项目中,但出现导入错误。无法导入模块。

npm install --save @nestjs/swagger@4.8.0 --force

我在收到错误后尝试了上述命令。删除并重新安装了 node_modules。什么都没有。

src/main.ts:2:44 - error TS1005: 'from' expected.

2 import { DocumentBuilder, SwaggerModule }  '@nestjs/swagger'
                                             ~~~~~~~~~~~~~~~~~

[7:47:36 PM] Found 1 error. Watching for file changes.

这是 main.ts 文件

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

import * as cookieParser from 'cookie-parser';


async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  const options = new DocumentBuilder()
    .setTitle('NestJS Middleware Test')
    .setDescription('Implemenet a simple requst interceptor for authorization')
    .setVersion('0.0.1')
    .build();

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

  app.use(cookieParser());
  await app.listen(3000);
}
bootstrap();

【问题讨论】:

  • 缺少关键字from。应该是import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'
  • 天啊。我应该删除这个问题。谢谢你。固定。

标签: typescript npm swagger nestjs nestjs-swagger


【解决方案1】:

您使用了错误的导入语法。即使在错误信息中,也可以看到'from' expected

正确的导入方式是

import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'

【讨论】:

  • VSCode 有时不能正常工作。到目前为止,所有的进口都还可以,但今天......
  • 嗯。这太奇怪了。你的意思是它是自动导入的?
猜你喜欢
  • 2023-01-31
  • 2020-06-30
  • 2021-11-06
  • 1970-01-01
  • 2022-12-30
  • 2021-12-03
  • 2021-10-18
  • 2020-05-22
  • 2012-11-06
相关资源
最近更新 更多