fastify-cookie: Property 'setCookie' does not exist on type 'FastifyReply'.

Installed fastify-cookie with npm install fastify-cookie.
Using VSCode.
fastify v.2.0.1 fastify-cookie v.3.0.2 error:

{
	"code": "2339",
	"message": "Property 'setCookie' does not exist on type 'FastifyReply<ServerResponse>'.",
	"source": "ts",
}

Code:

const fastifyCookie = require("fastify-cookie");
export const renderMiddleware = (fastify: FastifyInstance) => {
    fastify.register(fastifyCookie);
    fastify.post("/create-session-cookie", async (req, res) => {
       
        try {
            const sessionCookie = await thriftClient.createSession(
                "blabla",
                tokenExpDate,
                lastIp,
                tokenData,
                null
            );

            res.setCookie("sessionId", sessionCookie.sessionId, {}) < Throws error
                .status(200)
                .send(sessionCookie);
        } catch (e) {
            req.log.info(JSON.stringify(e));
            res.status(500).send(e);
            return;
        }
    });
}

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 4
  • Comments: 15 (7 by maintainers)

Most upvoted comments

@colinmcparland I got more time to look into this and see the real issue.

Change:

const fastifyCookie = require("fastify-cookie");

to this:

import fastifyCookie = require("fastify-cookie");

Import will make TS import the typings.

Guys, i has been fix it in my project. By default, typescript compile type by normal path, but im using pnpm which create symlinks, nah you must setting tsconfig to preserve symlinks, { "compilerOptions": { "preserveSymlinks": true, } }

fastify 4.22.2 still get this error

I’ve stumbled upon the same problem with the latest version of fastify (4.25.2 at the time of this writing) and @fastify/cookie: 9.3.1

My setup uses PNP which is known to break dependencies, so if your setup also uses PNP, the solution (at least for cookies) is to add this to your .yarnrc file

packageExtensions:
  '@fastify/cookie@*':
    dependencies:
      'fastify': '*'

this is because @fastify/cookie depends on fastify and uses module augmentation to declare the setCookie, clearCookie methods and others.

This workaround should also apply to other scenarios in a PNP project. Just make sure to add required dependencies in .yarnrc For more info, I’d suggest reading on how PNP works and why the dependency chain is broken

I have the same problem. Was this fixed?

TS2339: Property 'setCookie' does not exist on type 'FastifyReply<Server, IncomingMessage, ServerResponse, RouteGenericInterface, unknown>'.

"@nestjs/platform-fastify": "^8.4.5",
"fastify": "^3.29.0",
"fastify-cookie": "^5.7.0",
"typescript": "^4.7.2",

The package manager used does not affect the problem. I tried both npm and pnpm.

EDIT: Nevermind, I was using the wrong package because I was following NestJS docs where it states to use fastify-cookie. Needed to use @fastify/cookie

@evanshortiss amazing, that fixed the issue. Thanks!!

@colinmcparland can you try using import syntax instead? This works for me:

import * as fastifyCookie from "fastify-cookie";
import { FastifyInstance } from 'fastify'

export const renderMiddleware = (fastify: FastifyInstance) => {
    fastify.register(fastifyCookie);
    fastify.post("/create-session-cookie", async (req, res) => {
        const cookie = 'not a real cookie'

        res.setCookie("sessionId", cookie, {})
            .status(200)
            .send(cookie);
    });
}