Express route crashes because req.body is undefined
TypeError: 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.
Fix it fast
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); });2Match what you see, then jump to its fix
- 1
Content-Type is application/json, and req.body prints undefinedNo body-parsing middleware is installedFix - 2
express.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
Works for POST with data, crashes on GET, DELETE or an empty POSTThe request has no body at allFix
- 1
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
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
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/72Fix
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/profileConfirmed by expressjs.com
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
Back end
Uploaded files are missing from the request in Express
MulterError: LIMIT_UNEXPECTED_FILE
Express does not parse file uploads itself, and Multer only handles multipart/form-data.
4 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
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fix