TypeScript cannot find req.cookies in a NestJS controller
Property '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.
Fix it fast
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.ts2Match what you see, then jump to its fix
- 1
The controller has no import for Request, or it comes from another packageRequest is not imported from expressFix - 2
The type error stays after the import, or req.cookies is undefined at runtimecookie-parser and its types are not installed or not registeredFix - 3
Your main.ts uses FastifyAdapterThe app runs on Fastify, not ExpressFix
- 1
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 --noEmitConfirmed by docs.nestjs.com
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-parser2Fix
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/demoConfirmed by expressjs.com
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.ts2Fix
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 --noEmitConfirmed by docs.nestjs.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
TypeScript says req.user does not exist on the Express Request type
Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'
The Request type from Express has no user field until you declare one.
2 causes, each with a fixBack end
MikroORM refuses to use the global EntityManager
ValidationError: Using global EntityManager instance methods for context specific actions is disallowed
MikroORM no longer allows the global identity map.
3 causes, each with a fixBack end
NestJS app will not start: Cannot find module '/app/dist/main'
Cannot find module 'dist/main'
Node cannot find dist/main.js because the build did not put it there.
4 causes, each with a fixBack end
TypeError: something.default is not a constructor in a NestJS app
Exception has occurred: TypeError: xxx.default is not a constructor
TypeScript turns import X from 'pkg' into require('pkg').default when esModuleInterop is off, and a CommonJS package has no default, so new X() fails.
2 causes, each with a fixBack 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