Maaz Zindani

Loading the index…

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

Express warns 'body-parser deprecated undefined extended'

Errorbody-parser deprecated undefined extended

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

  • Applies to Express 4, Express 5, body-parser 1.x and 2.x, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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_modules
  2. 2Match what you see, then jump to its fix

    1. 1The warning prints once at startup, with a file and line number after iturlencoded() was called with no extended optionFix
    2. 2req.body shows keys like 'user[name]' instead of { user: { name } }You upgraded and nested form fields stopped parsingFix
    3. 3A TypeError about req.body being undefined, only on routes that receive no matching bodyreq.body is undefined after moving to body-parser 2Fix

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

2Fix

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

Confirmed by github.com

Did this fix it?

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

Did this fix it?

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

Did this fix it?

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

Next step

Want this handled for good?

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