Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extendedShort answer
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0. Pass extended explicitly. Use false for flat form fields and true only if you need nested objects like user[name]. In Express 5 and body-parser 2 the default is false.
Fix it fast
1Find out which cause you have
Find every place the urlencoded parser is created and see whether it passes an options object.
grep -rn "urlencoded" --include=*.js --include=*.ts . --exclude-dir=node_modules2Match what you see, then jump to its fix
- 1
The warning prints once at startup, with a file and line number after iturlencoded() was called with no extended optionFix - 2
req.body shows keys like 'user[name]' instead of { user: { name } }You upgraded and nested form fields stopped parsingFix - 3
A TypeError about req.body being undefined, only on routes that receive no matching bodyreq.body is undefined after moving to body-parser 2Fix
- 1
What does the error look like?
body-parser deprecated undefined extended
Cause 1 of 3
urlencoded() was called with no extended option
1Check
body-parser 1.4.0 deprecated calling urlencoded() without the extended option. Both bodyParser.urlencoded() and express.urlencoded() in Express 4 are affected.
grep -rn "urlencoded()" --include=*.js . --exclude-dir=node_modules
npm ls body-parser2Fix
Pass extended explicitly. false parses with Node's querystring library; true parses with qs and allows rich nested objects.
app.use(express.urlencoded({ extended: false }));
app.use(express.json());3Verify
Restart the app. The warning is gone.
node server.jsConfirmed by github.com
Cause 2 of 3
You upgraded and nested form fields stopped parsing
1Check
In Express 5 and body-parser 2 the extended option defaults to false. A form body of user[name]=Tobi then gives a key literally named user[name], while extended: true gives a nested object.
curl -s -X POST http://localhost:3000/test \
-d "user[name]=Tobi"2Fix
If your code relies on nested objects, set extended: true. If not, keep false and read the flat keys.
app.post('/test', express.urlencoded({ extended: true }), (req, res) => {
res.json(req.body); // { user: { name: 'Tobi' } }
});3Verify
The curl call returns the nested object.
curl -s -X POST http://localhost:3000/test -d "user[name]=Tobi"Confirmed by expressjs.com
Cause 3 of 3
req.body is undefined after moving to body-parser 2
1Check
In body-parser 2, req.body is left undefined when no body is parsed. Version 1 always set it to an empty object. This happens when the Content-Type does not match the parser's type option.
console.log(req.headers['content-type'], req.body);2Fix
Send the right Content-Type, register the parser for it, and read with a default.
const { name } = req.body ?? {};3Verify
A request with the matching Content-Type fills req.body.
curl -s -X POST http://localhost:3000/test -H "Content-Type: application/json" -d "{\"name\":\"a\"}"Confirmed by github.com
How do I stop it coming back?
- Always pass options explicitly to express.urlencoded() so a major upgrade does not change behaviour.
- Register express.json() and express.urlencoded() before your routes.
- Read req.body with a fallback when a route can be called without a body.
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
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
Browser blocks a stylesheet or script: MIME type mismatch with nosniff
The Network tab shows the asset URL answered with Content-Type text/html
With X-Content-Type-Options: nosniff the browser accepts a stylesheet only as text/css and a script only as a JavaScript type.
3 causes, each with a fixBack end
Discord OAuth2 login fails on the redirect_uri in an Express app
invalid_request
Discord compares the redirect_uri you send with the URL you registered for the application, and the token step must repeat the same value as the authorization step.
4 causes, each with a fix