Maaz Zindani

Loading the index…

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

Express route crashes because req.body is undefined

ErrorTypeError: Cannot destructure property 'userId' of 'req.body' as it is undefined.

Short answer

Express does not read request bodies on its own. req.body stays undefined until a body-parsing middleware such as express.json() runs and the request's Content-Type matches it. Add the parser before the route, send the header it expects, and guard routes that may get no body.

  • Applies to Express 4 and 5, Node.js 22, Postman
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print what the route really receives, including the header that decides whether any parser runs.

    app.post('/users', (req, res) => {
      console.log(req.headers['content-type'], req.body);
      res.sendStatus(204);
    });
  2. 2Match what you see, then jump to its fix

    1. 1Content-Type is application/json, and req.body prints undefinedNo body-parsing middleware is installedFix
    2. 2express.json() is installed, but the logged Content-Type is text/plain, multipart/form-data or missingThe Content-Type header does not match what the parser acceptsFix
    3. 3Works for POST with data, crashes on GET, DELETE or an empty POSTThe request has no body at allFix

What does the error look like?

  • TypeError: Cannot destructure property 'userId' of 'req.body' as it is undefined.

Cause 1 of 3

No body-parsing middleware is installed

1Check

Search the app for express.json or express.urlencoded. If neither appears, nothing fills req.body.

grep -rn "express.json\|express.urlencoded" --include=*.js --include=*.ts --exclude-dir=node_modules .

2Fix

Register the parsers on the app. Use express.json() for JSON and express.urlencoded() for HTML form posts.

const express = require('express');
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

3Verify

Send a JSON body and read it back.

curl -s -X POST http://localhost:3000/profile \
  -H "Content-Type: application/json" \
  -d '{"userId": 7}'

Confirmed by expressjs.com

Did this fix it?

Cause 2 of 3

The Content-Type header does not match what the parser accepts

1Check

express.json() only parses bodies whose Content-Type matches its type option, which defaults to application/json. In Postman a body set to raw Text or form-data sends a different type.

curl -s -i -X POST http://localhost:3000/profile -d '{"userId": 7}'

2Fix

Send the right header (in Postman choose Body, raw, JSON), or widen the parser's type option when you really use another JSON media type.

// accept a vendor JSON type as well
app.use(express.json({ type: ['application/json', 'application/vnd.api+json'] }));

3Verify

The log line now shows application/json and an object for req.body.

curl -s -X POST http://localhost:3000/profile \
  -H "Content-Type: application/json" -d '{"userId": 7}'

Confirmed by expressjs.com

Did this fix it?

Cause 3 of 3

The request has no body at all

1Check

With the current body-parser, req.body stays undefined when no body is parsed, for example when the request has neither Content-Length nor Transfer-Encoding. Older versions set it to an empty object, so code that worked once can break after an upgrade.

curl -s -i -X DELETE http://localhost:3000/profile/7

2Fix

Do not destructure req.body directly. Read it with optional chaining or a default, and validate the values.

app.post('/profile', (req, res) => {
  const { userId } = req.body ?? {};
  if (!userId) return res.status(400).json({ error: 'userId is required' });
  res.json({ userId });
});

3Verify

An empty request now gets a 400 instead of a crash.

curl -s -i -X POST http://localhost:3000/profile

Confirmed by expressjs.com

Did this fix it?

How do I stop it coming back?

  • Register express.json() once, near the top of the app, and keep one line per parser.
  • Treat req.body as untrusted: the Express docs warn its shape comes from user input, so validate each property.
  • Keep a saved Postman request or curl command per route, with the header set.

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

Next step

Want this handled for good?

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