Skip to content

Regsiter fastify plugin doesn't work #11265

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

Closed
4 of 15 tasks
deleau-victor opened this issue Mar 13, 2023 · 16 comments
Closed
4 of 15 tasks

Regsiter fastify plugin doesn't work #11265

deleau-victor opened this issue Mar 13, 2023 · 16 comments
Labels
needs triage This issue has not been looked into

Comments

@deleau-victor
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Hi i am trying to register the fastify plugins but there are errors and i don't know why because in my previous code i was doing the same thinks and was working.

import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'
import fastifyCookie, { FastifyCookie } from '@fastify/cookie'
import fastifyCors from '@fastify/cors'
import fastifyCsrfProtection from '@fastify/csrf-protection'
import fastifyHelmet from '@fastify/helmet'
import { ConfigService } from '@nestjs/config'
import {
	FastifyAdapter,
	NestFastifyApplication,
} from '@nestjs/platform-fastify'
import * as fs from 'fs'
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
import { ValidationPipe } from '@nestjs/common'
const httpsOptions = {
	key: fs.readFileSync('./secrets/cert.key'),
	cert: fs.readFileSync('./secrets/cert.crt'),
}

async function bootstrap() {
	const app = await NestFactory.create<NestFastifyApplication>(
		AppModule,
		new FastifyAdapter({ https: httpsOptions })
	)
	const configService = app.get(ConfigService)
	app.register(fastifyCookie, {
		secret: configService.get<string>('COOKIE_SECRET'),
	})
	app.register(fastifyHelmet)
	app.register(fastifyCsrfProtection, { cookieOpts: { signed: true } })
	app.register(fastifyCors, {
		credentials: true,
		origin: `https://${configService.get<string>('domain')}`,
	})
	app.useGlobalPipes(new ValidationPipe())
	await app.listen(3000)
	console.log(`Application is running on: ${await app.getUrl()}`)
}
bootstrap()

Like on this code, all registered plugins return error like :

'Argument of type 'FastifyCookie' is not assignable to parameter of type 'FastifyPluginCallback<FastifyCookieOptions, RawServerDefault, FastifyTypeProviderDefault, FastifyBaseLogger> | FastifyPluginAsync<...> | Promise<...> | Promise<...>'

Minimum reproduction code

https://github.com/deleau-victor/docchus-back

Steps to reproduce

Try to register any fastify plugin

Expected behavior

Nest JS must correctly register the fastify plugins

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

No response

Packages versions

"@fastify/cookie": "^8.3.0",
		"@fastify/cors": "^8.2.0",
		"@fastify/csrf-protection": "^6.1.0",
		"@fastify/helmet": "^10.1.0",
		"@fastify/static": "^6.6.1",
		"@nestjs/common": "^9.2.1",
		"@nestjs/config": "^2.2.0",
		"@nestjs/core": "^9.2.1",
		"@nestjs/mapped-types": "*",
		"@nestjs/platform-fastify": "^9.2.1",
		"@nestjs/swagger": "^6.1.4",
		"@nestjs/throttler": "^4.0.0",
		"bcrypt": "^5.1.0",
		"cache-manager": "^5.1.4",
		"cache-manager-ioredis-yet": "^1.1.0",
		"class-transformer": "^0.5.1",
		"class-validator": "^0.14.0",
		"dayjs": "^1.11.7",
		"fastify": "^4.12.0",
		"handlebars": "^4.7.7",
		"ioredis": "^5.2.5",
		"joi": "^17.7.0",
		"jsonwebtoken": "^9.0.0",
		"moment": "^2.29.4",
		"neo4j-driver": "^5.6.0",
		"nest-neo4j": "^0.2.0",
		"nodemailer": "^6.9.0",
		"reflect-metadata": "^0.1.13",
		"rxjs": "^7.8.0",
		"slugify": "^1.6.5",
		"uuid": "^9.0.0"

Node.js version

No response

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@deleau-victor deleau-victor added the needs triage This issue has not been looked into label Mar 13, 2023
@micalevisk
Copy link
Member

Hi!

Looks like your repository is private.

@quinguyenzx
Copy link

Won't happen if you install fastify@4.13.0.
I tested on my local machine, and this issue will happen on fastify versions:

  • 4.12.0
  • 4.14.0
  • 4.14.1 (latest)

The issue seems like mismatch interface:
Argument of type 'FastifyHelmet' is not assignable to parameter of type 'FastifyPluginCallback<FastifyHelmetOptions, RawServerDefault, FastifyTypeProviderDefault, FastifyBaseLogger> | FastifyPluginAsync<...> | Promise<...> | Promise<...>'.
image

@AsyncLegs
Copy link

AsyncLegs commented Oct 24, 2023

In my case, reverting the TypeScript version to 4.9 resolved the issue.

@joelywz
Copy link

joelywz commented Oct 28, 2023

Ensure that the Fastify version in the project matches the one specified in @nestjs/platform-fastify

@mxtheussouza
Copy link

I have this problem in recent versions.

@halecca
Copy link

halecca commented Jan 23, 2024

I have this problem in recent versions.

Have you found any solution?

@Der-Alex
Copy link

Der-Alex commented Feb 16, 2024

UPDATE:

I found out that the app.register function from the NestFastifyApplication is based on fastify@4.26.0 whereas @nestjs/platform-fastify seems to want to have fastify@4.26.1. They are not compatible. When I install fastify@4.26.0 I can register my plugins.

@deleau-victor Maybe someone from your nestjs team can check this up?

I am facing the same type incompatibility issue with the latest versions and pnpm:

@nestjs/core@10.3.3
@nestjs/platform-fastify@10.3.3 >> package.json says: fastify@4.26.1
fastify@4.26.1
@fastify/cookie@9.3.1

grafik

Any advice?

@halecca
Copy link

halecca commented Feb 16, 2024

UPDATE:

I found out that the app.register function from the NestFastifyApplication is based on fastify@4.26.0 whereas @nestjs/platform-fastify seems to want to have fastify@4.26.1. They are not compatible. When I install fastify@4.26.0 I can register my plugins.

@deleau-victor Maybe someone from your nestjs team can check this up?

I am facing the same type incompatibility issue with the latest versions and pnpm:

@nestjs/core@10.3.3
@nestjs/platform-fastify@10.3.3 >> package.json says: fastify@4.26.1
fastify@4.26.1
@fastify/cookie@9.3.1

grafik

Any advice?

For now this is the solution I have found, I have the same versions as you do.

Main.ts

await app.register(require('@fastify/cookie'))
await app.register(require('@fastify/csrf-protection'), {
	cookieKey: 'XSRF-TOKEN',
	cookieOpts: { signed: true },
})

await app.register(require('@fastify/helmet'))

and I had to disable this to stop slint from displaying that error, at the start of Main.ts

/* eslint-disable @typescript-eslint/no-var-requires */

@jmcdo29
Copy link
Member

jmcdo29 commented Feb 16, 2024

This is generally a typescript error due to the fact that fastify is most likely directly installed and installed via @nestjs/platform-fastify. If you install the same version that @nestjs/platform-fastify does, the error will go away

@richardanewman
Copy link

This is generally a typescript error due to the fact that fastify is most likely directly installed and installed via @nestjs/platform-fastify. If you install the same version that @nestjs/platform-fastify does, the error will go away

This worked for me. Changed package.json to "fastify": "4.26.0", to match the same version in @nestjs/platform-fastify and ran npm i -force and the error went away. Thanks! @jmcdo29

@alvinlys
Copy link

anyone tried the latest but failed too?
image

@viniciuskloppel
Copy link

viniciuskloppel commented May 19, 2024

Just adding "fastify": "4.26.1" to the devDependencies not necessarily will fix the problem. It actually depends on the version of fastify that your version of @nestjs/platform-fastify is using. Here's what I recommend:

  • Locate the package.json of the @nestjs/platform-fastify in the node_modules folder, i.e., node_modules/@nestjs/platform-fastify/package.json
  • Look what's the version of fastify being used in that file. Let's say it's "fastify": "4.17.0"
  • Go back to the package.json of your project (where the issue is happening)
  • Add the version of fastify you found above (e.g. "fastify": "4.17.0") to the devDependencies

To add more context: what's happening is that the other dependencies like @fastify/helmet and @fastify/compress use a caret range specifier for fastify, allowing for minor updates (it goes up to 4.26.2 at the time of this comment). But, the fastify version defined in the @nestjs/platform-fastify is a fixed value. That's why you get the type errors.

@XIYO
Copy link

XIYO commented Jul 21, 2024

I used a translator.

The solution I found was to ignore the type checking or make the versions match.

sol. 1

// @ts-ignore
await app.register(fastifyCookie);

sol. 2
match version
image

problem. 1
unmatch version
image


However, I have a question.

Types like FastifyReply and FastifyRequest obviously exist in the Fastify package, so it makes sense to add these dependencies additionally. Due to this, compatibility issues with versions can arise, forcing me to tediously update Fastify with every release.

While I gained type safety, I encountered compatibility issues. 😂

Is there a better way to alleviate this fatigue?

@puzzle9
Copy link

puzzle9 commented Sep 14, 2024

Is there any elegant way now

@suyash-purwar
Copy link

Ensure that the Fastify version in the project matches the one specified in @nestjs/platform-fastify

Thanks @joelywz. This fixed the issue for me. Adding this comment for visibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

17 participants