Search code examples
nestjshelmet.jsnestjs-fastify

type of `FastifyHelmet` is not compatible to FastifyPlugin in Nest JS


I'm new to NestJS Fastify. I use NestJS with express as a default before. I'd like to try using fastify this time, but got a type error when registering FastifyHelmet to app Instance.

*notes: I also new to ask question in this forum, so feel free to correct me.

I'm following the official docs like this:

import {
  FastifyAdapter,
  NestFastifyApplication,
} from '@nestjs/platform-fastify';

// inside bootstrap() function
const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(),
    {
      ...secureOptions,
      logger: new CustomLogger(),
      cors: true, // change this to Client IP when Production
    },
  );

await app.register(helmet);

I don't know if i missed something, but it shows a type error like this:

Argument of type 'typeof fastifyHelmet' is not assignable to parameter of type 'FastifyPluginCallback | FastifyPluginAsync | Promise<...> | Promise<...>'. Type 'typeof fastifyHelmet' is not assignable to type 'FastifyPluginCallback'. Types of parameters 'instance' and 'instance' are incompatible.

This is my current dependencies:

"dependencies":{
    "@fastify/helmet": "^11.1.1",
    "@nestjs/common": "^10.0.0",
    "@nestjs/config": "^3.1.1",
    "@nestjs/core": "^10.0.0",
    "@nestjs/platform-fastify": "^10.3.3",
    // other packages
}

Solution

  • After I research to github issues. I found out that the version of fastify is not match to @nestjs/platform-fastify.

    For now, as a temporary solution. I change the version of [email protected] to [email protected]

    Please refer to this link to read more.