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
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.
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.
“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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With