585

I'm receiving the following error with express:

Error: request entity too large
    at module.exports (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/node_modules/raw-body/index.js:16:15)
    at json (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/json.js:60:5)
    at Object.bodyParser [as handle] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:53:5)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.cookieParser [as handle] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/cookieParser.js:60:5)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.logger (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/logger.js:158:5)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
    at Object.staticMiddleware [as handle] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/middleware/static.js:55:61)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:193:15)
TypeError: /Users/michaeljames/Documents/Projects/Proj/mean/app/views/includes/foot.jade:31
    29| script(type="text/javascript", src="/js/socketio/connect.js")
    30| 
  > 31| if (req.host='localhost')
    32|     //Livereload script rendered 
    33|     script(type='text/javascript', src='http://localhost:35729/livereload.js')  
    34| 

Cannot set property 'host' of undefined
    at eval (eval at <anonymous> (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:152:8), <anonymous>:273:15)
    at /Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:153:35
    at Object.exports.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:197:10)
    at Object.exports.renderFile (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:233:18)
    at View.exports.renderFile [as engine] (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/jade/lib/jade.js:218:21)
    at View.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/lib/view.js:76:8)
    at Function.app.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/lib/application.js:504:10)
    at ServerResponse.res.render (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/lib/response.js:801:7)
    at Object.handle (/Users/michaeljames/Documents/Projects/Proj/mean/config/express.js:82:29)
    at next (/Users/michaeljames/Documents/Projects/Proj/mean/node_modules/express/node_modules/connect/lib/proto.js:188:17)

POST /api/0.1/people 500 618ms

I am using meanstack. I have the following use statements in my express.js

//Set Request Size Limit
app.use(express.limit(100000000));

Within fiddler I can see the content-length header with a value of: 1078702

I believe this is in octets, this is 1.0787 megabytes.

I have no idea why express is not letting me post the json array I was posting previously in another express project that was not using the mean stack project structure.

GG.
  • 17,726
  • 11
  • 69
  • 117
mike james
  • 6,780
  • 3
  • 16
  • 20
  • 5
    quick note on this to anyone coming to this question - make sure your issue is actually the node server or body parser. For example I'm using body parser correctly but I got this error because I for to set the max body size in the NGINX conf file. – Stephen Tetreault Dec 11 '17 at 18:14
  • @StephenTetreault I think you should add that as an answer, while of course it won't apply to everyone, it was exactly what was happening to me, kudos. – Dado Dec 16 '19 at 16:40

24 Answers24

1205

I had the same error recently, and all the solutions I've found did not work.

After some digging, I found that setting app.use(express.bodyParser({limit: '50mb'})); did set the limit correctly.

When adding a console.log('Limit file size: '+limit); in node_modules/express/node_modules/connect/lib/middleware/json.js:46 and restarting node, I get this output in the console:

Limit file size: 1048576
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
Limit file size: 52428800
Express server listening on port 3002

We can see that at first, when loading the connect module, the limit is set to 1mb (1048576 bytes). Then when I set the limit, the console.log is called again and this time the limit is 52428800 (50mb). However, I still get a 413 Request entity too large.

Then I added console.log('Limit file size: '+limit); in node_modules/express/node_modules/connect/node_modules/raw-body/index.js:10 and saw another line in the console when calling the route with a big request (before the error output) :

Limit file size: 1048576

This means that somehow, somewhere, connect resets the limit parameter and ignores what we specified. I tried specifying the bodyParser parameters in the route definition individually, but no luck either.

While I did not find any proper way to set it permanently, you can "patch" it in the module directly. If you are using Express 3.4.4, add this at line 46 of node_modules/express/node_modules/connect/lib/middleware/json.js :

limit = 52428800; // for 50mb, this corresponds to the size in bytes

The line number might differ if you don't run the same version of Express. Please note that this is bad practice and it will be overwritten if you update your module.

So this temporary solution works for now, but as soon as a solution is found (or the module fixed, in case it's a module problem) you should update your code accordingly.

I have opened an issue on their GitHub about this problem.

[edit - found the solution]

After some research and testing, I found that when debugging, I added app.use(express.bodyParser({limit: '50mb'}));, but after app.use(express.json());. Express would then set the global limit to 1mb because the first parser he encountered when running the script was express.json(). Moving bodyParser above it did the trick.

That said, the bodyParser() method will be deprecated in Connect 3.0 and should not be used. Instead, you should declare your parsers explicitly, like so :

app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb'}));

In case you need multipart (for file uploads) see this post.

[second edit]

Note that in Express 4, instead of express.json() and express.urlencoded(), you must require the body-parser module and use its json() and urlencoded() methods, like so:

var bodyParser = require('body-parser');
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));

If the extended option is not explicitly defined for bodyParser.urlencoded(), it will throw a warning (body-parser deprecated undefined extended: provide extended option). This is because this option will be required in the next version and will not be optional anymore. For more info on the extended option, you can refer to the readme of body-parser.

[third edit]

It seems that in Express v4.16.0 onwards, we can go back to the initial way of doing this (thanks to @GBMan for the tip):

app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb'}));
Samuel Bolduc
  • 15,054
  • 5
  • 31
  • 55
  • Hi Samuel, Thank you for your comprehensive answer! Yes your correct, this works for me also. I have placed a message on the express issue: https://github.com/visionmedia/express/issues/1793. Which is relevant to the changes being made by express. Hopefully they will make the change soon... Thanks for you help. This has been driving me mad! – mike james Nov 13 '13 at 22:14
  • Update - Whilst the above works, according to a comment on github. Setting bodyParser within app.use with no parameters sets the global limit to 1mb. update your global statement to express.bodyParser({limit:'100mb'}). Ensure you set the limit once. you can make a lower limit per route but cannot be larger than the global limit. I do wonder what the plan is for {limit:''} within connect as i'm being told in the console its being removed in connect 3.0? – mike james Nov 13 '13 at 22:26
  • 12
    My problem was that I had express.json() parser above my bodyParser... After understanding how it all works I removed bodyParser and set de limit in the other, like `app.use(express.json({limit:'50mb'}));`. I edited my answer to reflect this! – Samuel Bolduc Nov 14 '13 at 02:22
  • Cool. From what I gather the next version of express once connect is 3.0. the bodyParser will still exist but not call any related code to multipart form data types. – mike james Nov 14 '13 at 19:14
  • 3
    for express 4, the indicated code throws "body-parser deprecated undefined extended: provide extended option" on the `urlencoded` call – Mike 'Pomax' Kamermans Oct 04 '14 at 19:17
  • @Mike'Pomax'Kamermans Thank you for pointing that out. I actually only tested `bodyParser.json()` and (falsely) assumed it would work for `urlencoded`. I updated the answer to reflect this. – Samuel Bolduc Oct 06 '14 at 15:48
  • March 2015 here: For the debugging tip in this answer, the limiting is now checked (for me) at two locations in `node_modules/body-parser/node_modules/raw-body/index.js`. – Ben Mar 15 '15 at 20:16
  • Does this apply only for bodies or for the entire request or the url? – Josh C. Apr 01 '15 at 21:39
  • StackOverflow saved my life. As usual. :) – Rahul Desai Aug 20 '15 at 19:44
  • In case it helps anyone: I use a template which had "express.coffee" which set bodyParser after everything was up and running. Changing it there solved the issue. You should look for `rouge files setting bodyParser`. – Automatico Aug 30 '15 at 09:25
  • 3
    Thanks for updating this for express 4. You are a life saver! – Swills Nov 04 '15 at 06:00
  • Thanks for thorough explanation. After fixing one bug today, this error starts being emitted. Utilized proposed solution for Express 3.x and it works like a charm! – helvete Dec 01 '15 at 11:35
  • 47
    This is as comprehensive as this answer gets, so there is one more point for future reference that I ran into if you are using nginx as a reverse proxy in front of your node.js/express instance, which is recommended practice as well. Nginx will throw the same `413::Request Entity is too large` exception. It wont forward the request to your express app. So we need to set `client_max_body_size 50M;` in the nginx config OR a specific server config OR even a specific location tag will work. – Aukhan Feb 16 '16 at 17:49
  • Very awesome. Thank you!! – David J Barnes Apr 27 '16 at 15:09
  • 3
    Thank you samuel for this ! Saved me from a world of headache, Cheers and +1 for the comprehensive answer! – BastianBuhrkall May 20 '16 at 09:29
  • Hi I am also facing the same issue, with Oracle MCS. But i am not using expressJs. Could anyone help me also? – Arj 1411 Nov 10 '16 at 05:42
  • Thank you, all. Even though this post is now 3½ years old, it solved the 100KB cap of my file uploader. Please note that since I am using Express 4, the 'express.' prefix is no longer required, at least with my project architecture. I am using: app.use(bodyParser.json({limit: '30mb'})); – Michael M Feb 23 '17 at 22:49
  • Thanks a lot for updating the post. It saved my day. – Vikas Putcha Apr 13 '18 at 17:38
  • I get a `body-parser deprecated undefined extended: provide extended option webapp.js:208:30` – loretoparisi Jul 12 '18 at 16:57
  • 1
    @SamuelBolduc Express 4 no longer requires `body-parser` in order to parse `json` and `urlencoded` data. http://expressjs.com/en/4x/api.html#express.json – Aternus Jul 16 '18 at 09:34
  • I tried using this, But no working for me. express version 4.17 @SamuelBolduc – Monkey D. Luffy Feb 18 '20 at 15:43
  • Since Express 4.16 we canu use: `app.use(express.json({limit: '50mb'})); app.use(express.urlencoded({limit: '50mb'}));` See: http://expressjs.com/en/4x/api.html#express.urlencoded – GBMan Apr 03 '20 at 13:31
  • Just to add to the list... If you are using Firebase functions, the MAX limit is 10MB. You will actually see the limit getting overridden to 10MB. – m.spyratos May 08 '20 at 03:23
  • Thankyou, your solution helped me dubug my problem with vue storefront (vsf). Turns out only i had to update the config settings in local.json from 100kb to 10mb. – Sizzling Code Aug 04 '20 at 09:12
  • good job , thank you save my time – malik kurosaki Dec 11 '20 at 14:57
144

In my case it was not enough to add these lines :

var bodyParser = require('body-parser');
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));

I tried adding the parameterLimit option on urlencoded function as the documentation says and error no longer appears.

The parameterLimit option controls the maximum number of parameters that are allowed in the URL-encoded data. If a request contains more parameters than this value, a 413 will be returned to the client. Defaults to 1000.

Try with this code:

var bodyParser = require('body-parser');
app.use(bodyParser.json({limit: "50mb"}));
app.use(bodyParser.urlencoded({limit: "50mb", extended: true, parameterLimit:50000}));
slorenzo
  • 3,386
  • 2
  • 27
  • 44
60

If someone tried all the answers, but hadn't had any success yet and uses NGINX to host the site add this line to /etc/nginx/sites-available

client_max_body_size 100M; #100mb
bugwheels94
  • 26,775
  • 3
  • 35
  • 57
Alexander
  • 1,286
  • 1
  • 15
  • 21
  • 4
    This was my problem, thanks! One clue is that nginx's default is 1MB, so if you seem to be limited to that amount then it's probably nginx. Also, another clue: `body-parser` [will return](https://github.com/expressjs/body-parser#request-entity-too-large) a `limit`property and a `length` property in the error object (along with `status:413`). Nginx doesn't do this. So if you can't see those properties in the error object, it's probably a nginx limit. [Here's](https://stackoverflow.com/a/26717238/993683) how to change this nginx setting (config files most likely in `/etc/nginx/sites-available/`) –  Jun 02 '17 at 16:38
  • 2
    thank you so much for this answer i believe this answer should be upvoted because it is important , personally i forgot about nginx configuration ... again big thank you – Fadi Abo Msalam Mar 12 '18 at 20:21
  • How to set limit only in express? – аlex dykyі Oct 31 '18 at 14:32
  • @аlexdykyі Answer from Vivek22 : app.use(bodyParser({limit: '50mb'})); – Alexander Nov 01 '18 at 23:20
  • Thank you! I'd have been chasing the wrong solution for another 2 hours if I hadn't spotted this answer I reckon – James Flight May 20 '20 at 09:15
33

I don't think this is the express global size limit, but specifically the connect.json middleware limit. This is 100kb by default when you use express.bodyParser() and don't provide a limit option.

Try:

app.post('/api/0.1/people', express.bodyParser({limit: '5mb'}), yourHandler);
Peter Lyons
  • 131,697
  • 28
  • 263
  • 265
  • 1
    Hi - I am using the following: app.get('/api/0.1/people/', express.bodyParser({limit:'100mb'}), tracks.all); I am still receiving the same error... – mike james Nov 12 '13 at 20:50
  • the default limit is 100kbyte as it seems now https://github.com/expressjs/body-parser#limit – Qiong Wu Aug 30 '16 at 16:14
17

in my case .. setting parameterLimit:50000 fixed the problem

app.use( bodyParser.json({limit: '50mb'}) );
app.use(bodyParser.urlencoded({
  limit: '50mb',
  extended: true,
  parameterLimit:50000
}));
fredmaggiowski
  • 2,103
  • 2
  • 23
  • 42
Mohanad Obaid
  • 251
  • 3
  • 3
  • This worked for me. "Request entity too large" seems to refer to large strings (like JSON) too. I was posting ~20Kib JSON and had this problem (notice that default `body-parser` payload **size** limit is OK for me). Was solved with `parameterLimit` only (no need to set any `limit`s). – aesede Jul 20 '16 at 16:01
16

2016, none of the above worked for me until i explicity set the 'type' in addition to the 'limit' for bodyparser, example:

  var app = express();
  var jsonParser       = bodyParser.json({limit:1024*1024*20, type:'application/json'});
  var urlencodedParser = bodyParser.urlencoded({ extended:true,limit:1024*1024*20,type:'application/x-www-form-urlencoded' })

  app.use(jsonParser);
  app.use(urlencodedParser);
Morfinismo
  • 4,240
  • 4
  • 15
  • 31
user1709076
  • 2,066
  • 7
  • 31
  • 50
  • Yes, this is what I needed to do also. And it has been set up so you can drive the config settings from your app script. – Robb Sadler Jun 30 '16 at 13:35
  • 3
    did you mean `application/x-www-form-urlencoded` (rather than `application/x-www-form-urlencoding`)? – grenade Jun 15 '17 at 08:09
16

For express ~4.16.0, express.json with limit works directly

app.use(express.json({limit: '50mb'}));
Stenal P Jolly
  • 491
  • 4
  • 15
13

The following worked for me... Just use

app.use(bodyParser({limit: '50mb'}));

that's it.

Tried all above and none worked. Found that even though we use like the following,

app.use(bodyParser());
app.use(bodyParser({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb'}));

only the 1st app.use(bodyParser()); one gets defined and the latter two lines were ignored.

Refer: https://github.com/expressjs/body-parser/issues/176 >> see 'dougwilson commented on Jun 17, 2016'

Tunaki
  • 116,530
  • 39
  • 281
  • 370
Vivek22
  • 626
  • 1
  • 9
  • 18
  • 1
    THANK YOU! I also had an old app.use(bodyParser.json()); in my code before and indeed, only the first one is taken! – Nico Nov 24 '18 at 01:01
9

In my case the problem was on Nginx configuration. To solve it I have to edit the file: /etc/nginx/nginx.conf and add this line inside server block:

client_max_body_size 5M;

Restart Nginx and the problems its gone

sudo systemctl restart nginx
Jaime Fernandez
  • 860
  • 9
  • 7
8

After דo many tries I got my solution

I have commented this line

app.use(bodyParser.json());

and I put

app.use(bodyParser.json({limit: '50mb'}))

Then it works

Boaz
  • 17,982
  • 8
  • 55
  • 62
Maulik Patel
  • 604
  • 1
  • 8
  • 21
5

I've used another practice for this problem with multer dependancie.

Example:

multer = require('multer');

var uploading = multer({
  limits: {fileSize: 1000000, files:1},
});

exports.uploadpictureone = function(req, res) {
  cloudinary.uploader.upload(req.body.url, function(result) {
    res.send(result);
  });
};

module.exports = function(app) {
    app.route('/api/upload', uploading).all(uploadPolicy.isAllowed)
        .post(upload.uploadpictureone);
};
Quentin Malguy
  • 209
  • 3
  • 6
5

Little old post but I had the same problem

Using express 4.+ my code looks like this and it works great after two days of extensive testing.

var url         = require('url'),
    homePath    = __dirname + '/../',
    apiV1       = require(homePath + 'api/v1/start'),
    bodyParser  = require('body-parser').json({limit:'100mb'});

module.exports = function(app){
    app.get('/', function (req, res) {
        res.render( homePath + 'public/template/index');
    });

    app.get('/api/v1/', function (req, res) {
        var query = url.parse(req.url).query;
        if ( !query ) {
            res.redirect('/');
        }
        apiV1( 'GET', query, function (response) {
            res.json(response);
        });
    });

    app.get('*', function (req,res) {
        res.redirect('/');
    });

    app.post('/api/v1/', bodyParser, function (req, res) {
        if ( !req.body ) {
            res.json({
                status: 'error',
                response: 'No data to parse'
            });
        }
        apiV1( 'POST', req.body, function (response) {
            res.json(response);
        });
    });
};
4

A slightly different approach - the payload is too BIG

All the helpful answers so far deal with increasing the payload limit. But it might also be the case that the payload is indeed too big but for no good reason. If there's no valid reason for it to be, consider looking into why it's so bloated in the first place.

Our own experience

For example, in our case, an Angular app was greedily sending an entire object in the payload. When one bloated and redundant property was removed, the payload size was reduced by a factor of a 100. This significantly improved performance and resolved the 413 error.

Boaz
  • 17,982
  • 8
  • 55
  • 62
4

for me following snippet solved the problem.

var bodyParser = require('body-parser');
app.use(bodyParser.json({limit: '50mb'})); 
WasiF
  • 15,431
  • 9
  • 81
  • 100
3

In my case removing Content-type from the request headers worked.

carkod
  • 953
  • 10
  • 25
2

The better use you can specify the limit of your file size as it is shown in the given lines:

app.use(bodyParser.json({limit: '10mb', extended: true}))
app.use(bodyParser.urlencoded({limit: '10mb', extended: true}))

You can also change the default setting in node-modules body-parser then in the lib folder, there are JSON and text file. Then change limit here. Actually, this condition pass if you don't pass the limit parameter in the given line app.use(bodyParser.json({limit: '10mb', extended: true})).

Marco
  • 110
  • 1
  • 11
2

If you are using express.json() and bodyParser together it will give error as express sets its own limit.

app.use(express.json());
app.use(express.urlencoded({ extended: false }));

remove above code and just add below code

app.use(bodyParser.json({ limit: "200mb" }));
app.use(bodyParser.urlencoded({ limit: "200mb",  extended: true, parameterLimit: 1000000 }));
Tomislav Stankovic
  • 2,972
  • 9
  • 26
  • 36
2

I faced the same issue recently and bellow solution workes for me.

Dependency : 
express >> version : 4.17.1
body-parser >> version": 1.19.0
const express = require('express');
const bodyParser = require('body-parser');

const app = express(); 
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));

For understanding : HTTP 431

The HTTP 413 Payload Too Large response status code indicates that the request entity is larger than limits defined by server; the server might close the connection or return a Retry-After header field.

Forhad
  • 1,038
  • 15
  • 26
2

The setting below has worked for me

Express 4.16.1

app.use(bodyParser.json({ limit: '50mb' }))
app.use(bodyParser.urlencoded({
  limit: '50mb',
  extended: false,
}))

Nginx

client_max_body_size 50m
client_body_temp_path /data/temp
Kent
  • 41
  • 5
1

I too faced that issue, I was making a silly mistake by repeating the app.use(bodyParser.json()) like below:

app.use(bodyParser.json())
app.use(bodyParser.json({ limit: '50mb' }))

by removing app.use(bodyParser.json()), solved the problem.

WasiF
  • 15,431
  • 9
  • 81
  • 100
1

Express 4.17.1

app.use( express.urlencoded( {
    extended: true,
    limit: '50mb'
} ) )

Demo csb

antelove
  • 2,039
  • 16
  • 14
1

After trying everything in this post, i was unsuccessful. But I found a solution that worked for me. I was able to solve it without using the body-parser and only with the express. It looked like this:

const express = require('express');    

const app = express();
app.use(express.json({limit: '25mb'}));
app.use(express.urlencoded({limit: '25mb', extended: true}));

Don't forget to use extended: true to remove the deprecated message from the console.

0

For me the main trick is

app.use(bodyParser.json({
  limit: '20mb'
}));

app.use(bodyParser.urlencoded({
  limit: '20mb',
  parameterLimit: 100000,
  extended: true 
}));

bodyParse.json first bodyParse.urlencoded second

Nicollas
  • 192
  • 9
0

For those who start the NodeJS app in Azure under IIS, do not forget to modify web.config as explained here Azure App Service IIS "maxRequestLength" setting

KEMBL
  • 364
  • 5
  • 9