Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hard dependency on fastify #1414

Closed
mbeckenbach opened this issue Jul 2, 2021 · 6 comments
Closed

Hard dependency on fastify #1414

mbeckenbach opened this issue Jul 2, 2021 · 6 comments

Comments

@mbeckenbach
Copy link

mbeckenbach commented Jul 2, 2021

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Error: Module not found: Error: Can't resolve 'fastify-swagger' in '...\node_modules\@nestjs\swagger\dist'

Expected behavior

Should just work, when following the getting started guide :-)

Minimal reproduction of the problem with instructions

Set up nest using @nestjs/ng-universal with express and follow this guide https://docs.nestjs.com/openapi/introduction

What is the motivation / use case for changing the behavior?

I want to add openpi to my project. But when I follow the guide from docs i face the error above. I am not using fastify.

Environment


"@nestjs/common": "^7.0.0",
"@nestjs/swagger": "^4.8.2",
"@nguniversal/express-engine": "^12.1.0",
"express": "^4.15.2",
 "@angular/core": "~12.1.0",

For Tooling issues:
- Node version: v14.17.0
- Platform:  Windows

Others:
@micalevisk
Copy link
Member

Hi! As you can see here:

const httpAdapter = app.getHttpAdapter();
if (httpAdapter && httpAdapter.getType() === 'fastify') {
return this.setupFastify(path, httpAdapter, document);
}
return this.setupExpress(path, app, document, options);
fastify-swagger will be loaded only if your app is using fastify as a http adapter (ie, app.getHttpAdapter() will return an instance of FastifyAdapter). The getting started guide looks fine to me, I've already followed it with no worries.

@mbeckenbach
Copy link
Author

Hm... thats strange. Because i do not use fastify but still get to that error. As you can see below not even the package is installed.

I get this error as soon as I add this to my main.ts:

const config = new DocumentBuilder()
    .setTitle('Cats example')
    .setDescription('The cats API description')
    .setVersion('1.0')
    .addTag('cats')
    .build();
"dependencies": {
    "@angular/animations": "~12.1.0",
    "@angular/cdk": "^12.1.0",
    "@angular/common": "~12.1.0",
    "@angular/compiler": "~12.1.0",
    "@angular/core": "~12.1.0",
    "@angular/forms": "~12.1.0",
    "@angular/material": "^12.1.0",
    "@angular/platform-browser": "~12.1.0",
    "@angular/platform-browser-dynamic": "~12.1.0",
    "@angular/platform-server": "~12.1.0",
    "@angular/router": "~12.1.0",
    "@nestjs/common": "^7.0.0",
    "@nestjs/core": "^7.0.0",
    "@nestjs/jwt": "^7.2.0",
    "@nestjs/ng-universal": "^4.3.0",
    "@nestjs/passport": "^7.1.6",
    "@nestjs/platform-express": "^7.0.0",
    "@nestjs/typeorm": "^7.1.5",
    "@nguniversal/express-engine": "^12.1.0",
    "bcrypt": "^5.0.1",
    "class-transformer": "^0.2.3",
    "class-validator": "^0.9.1",
    "cookie-parser": "^1.4.5",
    "express": "^4.15.2",
    "helmet": "^4.6.0",
    "mssql": "^7.1.3",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0",
    "rand-token": "^1.0.1",
    "reflect-metadata": "^0.1.13",
    "rxjs": "~6.6.0",
    "tslib": "^2.0.0",
    "typeorm": "^0.2.34",
    "zone.js": "~0.11.4"
  },

@jmcdo29
Copy link
Member

jmcdo29 commented Jul 2, 2021

I'm not familiar with ng-universal, but I would bet there's a webpack bundler going on under the hood that is trying to resolve fastify-swagger for you . Can you provide a minimum reproduction?

@mbeckenbach
Copy link
Author

Yes it uses webpack for sure. I will push you something to GitHub tomorrow. Thank you in advance

@mbeckenbach
Copy link
Author

@jmcdo29 I pushed it to https://github.com/mbeckenbach/nestswagrepro

It will show you the error. The nestjs code base is in the /server folder.
I have added the quick start guide code to server/main.ts in line 12 to 19.

There is a custom-webpack.config.js in the project root used by the angular compiler to suppress some warnings from typeorm. It simply extends the webpack config that angular uses under the hood.

npm run dev:ssr to start it.
Will launch on localhost:4200

I think you don't need it, but just in case: this is an angular project using @angular/cli which i have installed globally.
I run on node 14.17.0

Thanks again for having a look!

@kamilmysliwiec
Copy link
Member

#1334 (comment)

Long story short: webpack tries to bundle all deps referenced in your code (and libraries you use), even if you're not actually using them (static analysis). Therefore, you must explicitly exclude these that you don't use if you want to bundle your application. For this, you'll have to provide a custom webpack configuration (setting the fastify-swagger as an external package using externals: []). You can find somewhat more complex example here nestjs/ng-universal#115

@nestjs nestjs locked and limited conversation to collaborators Jul 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants