Search code examples
typescriptnpmswaggernestjsnestjs-swagger

NestJS / Swagger - Can't Import the Module


I am creating a boilerplate nestjs app. I want to add @nestjs/swagger to project but getting import error. Can't import the module.

npm install --save @nestjs/[email protected] --force

I tried above command after getting errors. Deleted and reinstalled the node_modules. Nothing is working.

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.

Here is the main.ts file

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();

Solution

  • You have used the wrong importing syntax. Even in the error message, you can see 'from' expected.

    The correct way to import is

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