Maaz Zindani

Loading the index…

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

TypeScript says req.user does not exist on the Express Request type

ErrorProperty '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.

  • Applies to TypeScript 5, Express 4, @types/express 5, ts-node 10, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check whether any file of yours already declares the property.

    grep -rn "interface Request" src types
  2. 2Match what you see, then jump to its fix

    1. 1The error appears on req.user although a middleware sets it at run timeNothing declares the user propertyFix
    2. 2The augmentation exists, tsc passes, but ts-node still reports the same errorThe declaration file is not part of the programFix

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" src

2Fix

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 --noEmit

Confirmed by typescriptlang.org

Did this fix it?

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 --showConfig

2Fix

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.ts

Confirmed by typestrong.org

Did this fix it?

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:

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.