413 Error: request entity too large in Node.js & Express

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
3 min

banner

# Table of Contents

  1. 413 Error: request entity too large in Node.js & Express
  2. Set the client_max_body_size variable if you use Nginx

# 413 Error: request entity too large in Node.js & Express

To solve the error "413: request entity too large" in Node.js and Express:

  1. Increase the limit of the request body size when configuring the body-parser module.
  2. Set the parameterLimit property to increase the maximum number of parameters that are allowed in the URL-encoded data.
index.js
import express from 'express'; import bodyParser from 'body-parser'; const app = express(); app.use(bodyParser.json({limit: '35mb'})); app.use( bodyParser.urlencoded({ extended: true, limit: '35mb', parameterLimit: 50000, }), );
The code for this article is available on GitHub

Make sure that the two app.use() calls are added before you define any routes.

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
const express = require('express'); const bodyParse = require('body-parser')

The limit property is used to control the maximum request body size.

By default, the limit property is set to 100kb, so the "request entity too large" error is raised if the user tries to upload a file that is greater than 100 kilobytes.

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

You can set the property to a value that suits your use case, e.g. 5mb or 10mb.

The parameterLimit property is used to control the maximum number of parameters that are allowed in the URL-encoded data.

If a request contains more parameters than the value of parameterLimit, then a "413: request entity too large" error is raised.

By default, the property is set to 1000.

Also, 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

make sure body parser module is installed

The code for this article is available on GitHub

# Set the client_max_body_size variable if you use Nginx

If you use Nginx in front of your Express.js server and the error persists, add the following line to your Nginx configuration file, e.g. /etc/nginx/sites-available or /etc/nginx/nginx.conf.

You can try to search for the client_max_body_size variable to see if it's defined and you can update its value to increase the max request body size.

/etc/nginx/nginx.conf
# 80MB request body max client_max_body_size 80M;

Your Nginx configuration files should be located in the /etc/nginx/ directory.

The default request body max size (client_max_body_size) in Nginx is 1 megabyte.

If the request body size exceeds the value of your client_max_body_size setting, then the "413::Request Entity is too large" exception is raised.

Restart your Nginx server after making the change.

shell
sudo systemctl restart nginx # or sudo service nginx restart
The code for this article is available on GitHub

Once you increase the max request body size in the configuration of the body-parser module and in your Nginx config, the issue will be resolved.

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