Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

TypeScript cannot find req.cookies in a NestJS controller

ErrorProperty 'cookies' does not exist on type 'Request'.

Short answer

TypeScript says 'cookies' is missing because the Request type in your file is not Express's, or because cookie support is not installed. Import Request from 'express', install cookie-parser and @types/cookie-parser, and register the middleware. On Fastify, type the request as FastifyRequest and register @fastify/cookie instead.

  • Applies to NestJS 11, TypeScript 5, Express 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the controller and look at where Request comes from. No import line at all means TypeScript is using a different Request.

    grep -n "Request" src/**/*.controller.ts
  2. 2Match what you see, then jump to its fix

    1. 1The controller has no import for Request, or it comes from another packageRequest is not imported from expressFix
    2. 2The type error stays after the import, or req.cookies is undefined at runtimecookie-parser and its types are not installed or not registeredFix
    3. 3Your main.ts uses FastifyAdapterThe app runs on Fastify, not ExpressFix

What does the error look like?

  • Property 'cookies' does not exist on type 'Request'.
  • error TS2339: Property 'cookies' does not exist on type

Cause 1 of 3

Request is not imported from express

1Check

NestJS docs show the parameter typed with Request from 'express'. Without that import, the name points to some other type that has no cookies property.

grep -rn "from 'express'" src/

2Fix

Import the type from express and use it on the @Req() parameter.

import { Controller, Get, Req } from '@nestjs/common';
import type { Request } from 'express';

@Controller('demo')
export class DemoController {
  @Get()
  readAll(@Req() req: Request) {
    return req.cookies;
  }
}

3Verify

Compile; TS2339 should be gone.

npx tsc --noEmit

Confirmed by docs.nestjs.com

Did this fix it?

Cause 2 of 3

cookie-parser and its types are not installed or not registered

1Check

Check that both packages are in package.json and that main.ts registers the middleware.

npm ls cookie-parser @types/cookie-parser

2Fix

Install the middleware and its types, then register it once in main.ts.

npm i cookie-parser
npm i -D @types/cookie-parser

// main.ts
import cookieParser from 'cookie-parser';
app.use(cookieParser(process.env.COOKIE_SECRET));

3Verify

Start the app, send a request with a cookie and read it back.

curl -s -H 'Cookie: a=1' http://localhost:3000/demo

Confirmed by expressjs.com

Did this fix it?

Cause 3 of 3

The app runs on Fastify, not Express

1Check

Express types and cookie-parser do not apply to the Fastify adapter.

grep -rn "FastifyAdapter" src/main.ts

2Fix

Register @fastify/cookie and type the request as FastifyRequest.

npm i @fastify/cookie

// main.ts
import fastifyCookie from '@fastify/cookie';
await app.register(fastifyCookie, { secret: process.env.COOKIE_SECRET });

// controller
import type { FastifyRequest } from 'fastify';
readAll(@Req() req: FastifyRequest) { return req.cookies; }

3Verify

Compile and request the route with a Cookie header.

npx tsc --noEmit

Confirmed by docs.nestjs.com

Did this fix it?

How do I stop it coming back?

  • Use the same HTTP platform (Express or Fastify) in types, middleware and adapter.
  • Add cookie packages and their types in one commit so the compiler and runtime agree.
  • Keep the cookie secret in an environment variable, not in code.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

TS1259: module can only be default-imported with esModuleInterop

error TS1259: Module '"./node_modules/@types/express/index"' can only be default-imported using the 'esModuleInterop' flag

TS1259 means you wrote a default import for a CommonJS module that TypeScript does not treat as having one.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.