Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MODULE_NOT_FOUND Nestjs and Swagger

I'm trying to add Swagger to my Nestjs app. Module not found error is thrown when I'm trying to compile it. I use the same code from Nestjs documentation. This is my main.ts:

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

  const config = new DocumentBuilder()
    .setTitle('Cats example')
    .setDescription('The cats API description')
    .setVersion('1.0')
    .addTag('cats')
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('api', app, document);

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

This is the error:

internal/modules/cjs/loader.js:883
  throw err;
  ^

Error: Cannot find module '@nestjs/core/router/route-path-factory'
Require stack:
- D:\BK\solidity\MVPApp\blockchain\back-end-student-wallet-v2\node_modules\@nestjs\swagger\dist\swagger-explorer.js
- D:\BK\solidity\MVPApp\blockchain\back-end-student-wallet-v2\node_modules\@nestjs\swagger\dist\swagger-scanner.js 
- D:\BK\solidity\MVPApp\blockchain\back-end-student-wallet-v2\node_modules\@nestjs\swagger\dist\swagger-module.js  
- D:\BK\solidity\MVPApp\blockchain\back-end-student-wallet-v2\node_modules\@nestjs\swagger\dist\index.js
- D:\BK\solidity\MVPApp\blockchain\back-end-student-wallet-v2\node_modules\@nestjs\swagger\index.js
- D:\BK\solidity\MVPApp\blockchain\back-end-student-wallet-v2\dist\main.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)

I'm using Node 14.15.1, @nestjs/swagger 5.0.0, swagger-ui-express: 4.1.6

like image 456
Nhân Đặng Avatar asked Jul 11 '21 08:07

Nhân Đặng


People also ask

Is Swagger V5 compatible with nest V8?

Swagger v5 is compatible with Nest v8 (@nestjs/core@^8.0.0, @nestjs/common@^8.0.0 etc) Swagger v4 is compatible with Nest v7 Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research! But avoid … Asking for help, clarification, or responding to other answers.

What is nestjs and how to use it?

Nestjs, not like any other nodejs frameworks, has many handy tools, libraries and features that let us write our programs following clean code and scalable architecture. In this article we’ll find out how to use swagger for documentation of our application, and also know the best practices for module creation.

What is the difference between “providers” and “exports” in nestjs?

“providers” is an array of classes that are used in our module. On the compiling stage nestjs will create objects automatically with resolving constructor dependencies. “exports” - in this section you can specify providers classes that can be used in providers other modules.

How do I use Swagger to test OpenAPI?

As a practical use case, Swagger also serves up the JSON file that represents the OpenAPI document for your API (for this demo, navigate to http://localhost:3000/swagger-json to see the output). This JSON file can be imported into Postman to create a new collection that can be used to test your API.


2 Answers

Update latest version of @nestjs/platform-express, @nestjs/common,@nestjs/core (8.0.0) solve my problem. It seems like nestjs/cli uses previous version of nestjs

like image 121
Nhân Đặng Avatar answered Oct 21 '22 03:10

Nhân Đặng


Swagger v5 is compatible with Nest v8 (@nestjs/core@^8.0.0, @nestjs/common@^8.0.0 etc) Swagger v4 is compatible with Nest v7

Source from https://github.com/nestjs/nest/issues/7499

like image 24
passport4j Avatar answered Oct 21 '22 01:10

passport4j