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>>'Short answer
The Request type from Express has no user field until you declare one. Add it through the open Express.Request interface with a global augmentation in a .d.ts or module file, and make sure the compiler loads that file. ts-node needs the files option for that.
Fix it fast
1Find out which cause you have
Check whether any file of yours already declares the property.
grep -rn "interface Request" src types2Match what you see, then jump to its fix
What does the error look like?
Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'
Cause 1 of 2
Nothing declares the user property
1Check
The typings keep an empty Express.Request interface that is meant to be extended by declaration merging, and the exported Request extends it. TypeScript only knows what is declared.
grep -rn "req.user" src2Fix
Declare the field in a global augmentation. A file with an import or export is a module; the declaration then merges into the global Express namespace.
// src/types/express.d.ts
export {};
declare global {
namespace Express {
interface Request {
user?: { id: string; email: string };
}
}
}3Verify
Type-check the project.
npx tsc --noEmitConfirmed by typescriptlang.org
Cause 2 of 2
The declaration file is not part of the program
1Check
tsconfig include decides which files are compiled; its default is all files when files is not set. ts-node does not load files, include or exclude from tsconfig unless you ask.
npx tsc --showConfig2Fix
Run ts-node with the files option, or set TS_NODE_FILES. Also check that the new path is covered by include.
npx ts-node --files src/index.ts
// tsconfig.json
{ "include": ["src/**/*"] }3Verify
Start the app again; the error should be gone.
npx ts-node --files src/index.tsConfirmed by typestrong.org
How do I stop it coming back?
- Keep one folder for hand-written declarations and keep it inside include.
- Make the field optional (user?) because it is empty before authentication runs.
- Use the same property type in the auth middleware and in the augmentation.
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.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
Related fixes
Back 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
TypeScript cannot find req.cookies in a NestJS controller
error TS2339: Property 'cookies' does not exist on type
TypeScript says 'cookies' is missing because the Request type in your file is not Express's, or because cookie support is not installed.
3 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 fixBack end
A namespace-style import cannot be called or constructed (import * as express)
A namespace-style import cannot be called or constructed, and will cause a failure at runtime
A namespace import (import * as x) is by spec an object, never a function.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
TypeScript cannot find '@angular/platform-browser' in an Angular project
Cannot find module '@angular/platform-browser' or its corresponding type declarations.
TS2307 means TypeScript could not turn the import into a file.
3 causes, each with a fix