Express rejects a request body as too large
HTTP 413 returned by Express when a JSON or form body is larger than the limit optionShort answer
A 413 means a body was bigger than a size limit. In Express, express.json and express.urlencoded accept 100kb by default, so raise the limit option on the parser that handles that content type. If the error page is from nginx, not Express, raise client_max_body_size instead.
Fix it fast
1Find out which cause you have
Find out who answered. Express returns the error from its error handler, while nginx returns its own HTML page. The Server header and the body tell them apart.
curl -i -X POST http://localhost:3000/api/upload \ -H "Content-Type: application/json" \ --data-binary @big.json | head -n 152Match what you see, then jump to its fix
- 1
413 with request entity too large text from Express, for JSON or form posts bigger than 100kbexpress.json or express.urlencoded still has the default limitFix - 2
You raised a limit but the error stays; two parsers are mounted for the same content typeA second parser is registered earlier with the default limitFix - 3
The response is an nginx HTML page saying 413 Request Entity Too Large, and Express logs nothingnginx in front of the app has its own 1 MB body limitFix
- 1
What does the error look like?
HTTP 413 returned by Express when a JSON or form body is larger than the limit option413 (Request Entity Too Large) returned by nginx in front of the app
Cause 1 of 3
express.json or express.urlencoded still has the default limit
1Check
Both parsers have a limit option that defaults to 100kb. Large JSON bodies, base64 images in JSON and big forms are the usual cases.
grep -rn "express.json\|express.urlencoded\|bodyParser" --include=*.js .2Fix
Set limit to the largest body you really want to accept, not to a huge number.
app.use(express.json({ limit: '5mb' }));
app.use(express.urlencoded({ extended: true, limit: '5mb' }));3Verify
Post the same body again; the status is no longer 413.
curl -s -o /dev/null -w "%{http_code}\n" -X POST http://localhost:3000/api/upload \
-H "Content-Type: application/json" --data-binary @big.jsonConfirmed by expressjs.com
Cause 2 of 3
A second parser is registered earlier with the default limit
1Check
Each parser only handles requests whose Content-Type matches its type option, and the first one that matches reads the body. If a default-limit express.json comes first, your larger one never sees the request.
grep -rn "express.json\|express.urlencoded" --include=*.js . | sort2Fix
Keep one parser per content type and give it the limit. For a single heavy route, mount a larger parser on that route only, before the global one is reached.
const bigJson = express.json({ limit: '20mb' });
app.post('/api/import', bigJson, importHandler);
app.use(express.json());3Verify
Post a large body to the route that needs it and a large one to another route; only the first is accepted.
curl -s -o /dev/null -w "%{http_code}\n" -X POST http://localhost:3000/api/import \
-H "Content-Type: application/json" --data-binary @big.jsonConfirmed by expressjs.com
Cause 3 of 3
nginx in front of the app has its own 1 MB body limit
1Check
nginx refuses bodies above client_max_body_size, which is 1m unless you changed it. The request never reaches Node.js.
sudo nginx -T 2>/dev/null | grep -n client_max_body_size2Fix
Set a limit in the server or location block that matches what Express accepts, then reload.
server {
client_max_body_size 5m;
}3Verify
Test the config, reload, and post the large body through the public address.
sudo nginx -t && sudo systemctl reload nginx
curl -s -o /dev/null -w "%{http_code}\n" -X POST https://example.com/api/upload \
-H "Content-Type: application/json" --data-binary @big.jsonConfirmed by nginx.org
How do I stop it coming back?
- Set limits on purpose: small on public JSON routes, larger only on the upload routes that need it.
- Send files as multipart or direct-to-storage uploads, not as base64 inside JSON.
- Keep the Express limit and the nginx client_max_body_size in the same file of notes so they change together.
- Return a clear message from your error handler when the status is 413.
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
Related fixes
Servers
nginx 502 Bad Gateway in front of a Node.js app
connect() failed (111: Connection refused) while connecting to upstream
A 502 means nginx reached for your Node.js app and got nothing usable back.
5 causes, each with a fixBack end
Multer upload fails with 'Unexpected end of form'
Error: Unexpected end of form
Multer reads multipart bodies with busboy, which raises this error when the request body ends before the closing boundary line.
2 causes, each with a fixBack end
Socket.IO answers 404 on the polling request
Cannot GET /socket.io/
A 404 on /socket.io/?EIO=4&transport=polling means the request is not reaching a Socket.IO server.
3 causes, each with a fixBack 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 fix