Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrong signature for req.body and next() #6370

Open
its-dibo opened this issue Mar 2, 2025 · 9 comments
Open

wrong signature for req.body and next() #6370

its-dibo opened this issue Mar 2, 2025 · 9 comments
Labels
awaiting more info typescript Topics related to TypeScript

Comments

@its-dibo
Copy link

its-dibo commented Mar 2, 2025

This is a regression in v5

import { NextFunction, Request, Response } from 'express';

export function someMiddleware(){
  return (req: Request, res: Response, next: NextFunction) => {
  
// Property 'body' does not exist on type 'Request<core.ParamsDictionary, any, any, core.Query, Record<string, any>>'.ts(2339)
req.body

// Property 'method' does not exist on type 'Request<core.ParamsDictionary, any, any, core.Query, Record<string, any>>'.ts(2339)
req.method 

// This expression is not callable.
// Type 'NextFunction' has no call signatures.ts(2349)
next()
}
}

Environment information

Version:

"dependencies": {
  "express": "^5.0.1",
"@types/express": "^5.0.0",
}

Platform:
Linux 3f32d8f1babe 6.11.0-17-generic #17~24.04.2-Ubuntu SMP PREEMPT_DYNAMIC Mon Jan 20 22:48:29 UTC 2 x86_64 GNU/Linux

Node.js version:
v22.12.0

@its-dibo its-dibo added the bug label Mar 2, 2025
@bjohansebas bjohansebas added typescript Topics related to TypeScript require-triage labels Mar 2, 2025
@wesleytodd wesleytodd removed the bug label Mar 4, 2025
@dpopp07
Copy link
Contributor

dpopp07 commented Mar 4, 2025

I'm unable to reproduce. What version of Typescript are you using? Will you confirm your Express dependency versions?

npm ls express @types/express typescript

@aramis-it
Copy link

aramis-it commented Mar 6, 2025

I think problem is:
link

req.body
The req.body property returns undefined when the body has not been parsed. In Express 4, it returns {} by default.

In my case get request body undefined. I don't know why body not parsed.
I solved this for my case:

      if (!req.body) {
          req.body = {}
      }

@bjohansebas
Copy link
Member

If the body is not sent in the response, it will be undefined. That is one of the breaking changes that were made. Check the migration guide.

@its-dibo
Copy link
Author

its-dibo commented Mar 9, 2025

In my case get request body undefined. I don't know why body not parsed. I solved this for my case:

This is a breaking change, the body now is undefined instead of {}

@its-dibo
Copy link
Author

its-dibo commented Mar 9, 2025

What version of Typescript are you using?

$ pnpm ls express @types/express typescript --filter express
Legend: production dependency, optional only, dev only

@engineers/express@1.1.1 /workspaces/dibo/packages/express

dependencies:
express 5.0.1

devDependencies:
@types/express 5.0.0

@dpopp07
Copy link
Contributor

dpopp07 commented Mar 10, 2025

This is a breaking change, the body now is undefined instead of {}

Correct - it is a breaking change introduced in v5 that is documented in the migration guide. I believe the purpose is to disambiguate between the user providing an empty body, and the app not parsing the body.

$ pnpm ls express @types/express typescript --filter express

By filtering on "express", the Typescript version is not shown 🙂

@its-dibo
Copy link
Author

its-dibo commented Mar 11, 2025

currently I don't use typescript in this package at all, as it meant to be imported directly in other apps without building, however I installed typescript but the same error still occurs

$ pnpm ls express @types/express typescript --filter express
Legend: production dependency, optional only, dev only

@engineers/express@1.1.1 /workspaces/dibo/packages/express

dependencies:
express 5.0.1

devDependencies:
@types/express 5.0.0
typescript 5.8.2

Env:

  • Node: v22.14.0
  • pnpm: 10.6.2
package.json
  "dependencies": {
    "express": "^5.0.1",
    "jsonwebtoken": "^9.0.2",
    "multer": "1.4.4",
    "parse-domain": "^8.2.2"
  },
  "devDependencies": {
    "@types/express": "^5.0.0",
    "@types/jsonwebtoken": "^9.0.7",
    "@types/multer": "^1.4.12",
    "semantic-release": "^24.2.1",
    "semantic-release-monorepo": "^8.0.2",
     "typescript": "^5.8.2"
  }

@its-dibo
Copy link
Author

I noticed that the problem occurs when using pnpm not npm

@NeutronDisk

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting more info typescript Topics related to TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants