body-parser deprecated undefined extended: provide extended option

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
3 min

banner

# body-parser deprecated undefined extended: provide extended option

The Express.js warning "body-parser deprecated undefined extended: provide extended option" occurs when you forget to explicitly set the extended property in the call to bodyParser.urlencoded.

To resolve the issue, set the extended property to a boolean value.

body parser is deprecated undefined extended provide

Here is an example of when the warning is shown.

index.js
import express from 'express'; import bodyParser from 'body-parser'; const app = express(); app.use(bodyParser.json()); app.use( bodyParser.urlencoded(), );

Here is the output of running the code.

shell
body-parser deprecated undefined extended: provide extended option file:/home/borislav/Desktop/bobbyhadz-js/index.js:9:14

In order to resolve the issue, set the extended property in the options object of the bodyParser.urlencoded() method.

index.js
import express from 'express'; import bodyParser from 'body-parser'; const app = express(); app.use(bodyParser.json()); app.use( bodyParser.urlencoded({ extended: true, }), );
The code for this article is available on GitHub

The code sample above uses the ES6 modules import/export syntax.

If you use the CommonJS require() syntax, use the following import statements instead.

index.js
// ๐Ÿ‘‡๏ธ using CommonJS require() const express = require('express'); const bodyParser = require('body-parser');

Make sure you have the body-parser module installed.

shell
# If you need to generate a package.json file npm init -y # ๐Ÿ‘‡๏ธ with NPM npm install express body-parser # or with YARN yarn add express body-parser

If you use an Express.js version greater than 4.16, you can also use the express.urlencoded() method which uses the body-parser module under the hood.

index.js
import express from 'express'; // ๐Ÿ‘‡๏ธ If you use CommonJS require() // const express = require('express') const app = express(); app.use(express.json()); app.use( express.urlencoded({ extended: true, }), );
The code for this article is available on GitHub

If you use an older version of Express, you can update the module by running the following command.

shell
# with NPM npm install express@latest # or with YARN yarn add express@latest

Make sure to restart your Express.js server after making the change.

The extended option allows you to choose between parsing the URL-encoded data with the querystring library when false or with the qs library when true.

The extended option defaults to true but using the default value has been deprecated.

The extended syntax allows for objects and arrays to be encoded into the URL-encoded format for a JSON-like experience.

It's better to set the extended argument to true to use the qs module because the querystring module has been deprecated as shown in the package's NPM page.

The qs module is actively maintained and is used for query string parsing and stringifying.

Here is a complete example of setting up an Express.js server and configuring it correctly.

index.js
import express from 'express'; import bodyParser from 'body-parser'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() // const express = require('express'); // const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.json()); app.use( bodyParser.urlencoded({ extended: true, }), ); app.post('/register', function (req, res) { console.log( `username: ${req.body.username}, password: ${req.body.password}`, ); res.send( `username: ${req.body.username}, password: ${req.body.password}`, ); }); const port = 5000; app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
The code for this article is available on GitHub

Assuming you save the code in an index.js file, you can run the following command to start the Express.js server.

shell
npx nodemon index.js

You can use Postman or a different HTTP client to send a POST request to http://localhost:5000/register where you specify the username and password parameters in the request body.

make http post request to express server

As noted previously, if you use an Express.js version greater than 4.16, you can use express.json() and express.urlencoded() middlewares.

index.js
import express from 'express'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() // const express = require('express') const app = express(); app.use(express.json()); app.use( express.urlencoded({ extended: true, }), );
The code for this article is available on GitHub

Starting with Express 4.16, the body-parser module has been included in the library, so you don't have to import it separately.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev