This error occurs when a POST or PUT request sends more data than Express's body-parser middleware allows. By default, Express limits JSON payloads to 100KB. Increase the limit in your middleware configuration to fix this.
The Express body-parser middleware enforces a maximum request body size to protect against memory exhaustion attacks and prevent inadvertent resource consumption. When a client sends a request exceeding this limit, the middleware rejects it with a 413 (Payload Too Large) HTTP status code. This is a safety mechanism that needs to be adjusted when you legitimately need to accept larger payloads.
The error message specifies which middleware is hitting the limit. Most commonly it's express.json() for JSON payloads or express.urlencoded() for form data. Check your Express app initialization.
If you're receiving JSON data (most common case), update your middleware configuration:
const express = require('express');
const app = express();
// Increase JSON payload limit to 50MB
app.use(express.json({ limit: '50mb' }));
app.post('/api/data', (req, res) => {
// Now accepts payloads up to 50MB
res.json({ received: req.body });
});
app.listen(3000);Common limit sizes:
- '100kb' - Default
- '1mb' - Small files and bulk data
- '50mb' - Large images and documents
- '500mb' - Video files (use with caution)
If your endpoint accepts URL-encoded form data, also update this middleware:
app.use(express.urlencoded({ limit: '50mb', extended: true }));The extended: true option allows parsing of rich objects and arrays in form data. Use it alongside express.json() to handle both content types.
If you're handling file uploads with multer, increase its limits separately:
const multer = require('multer');
const upload = multer({
limits: {
fileSize: 50 * 1024 * 1024, // 50MB in bytes
},
});
app.post('/upload', upload.single('file'), (req, res) => {
res.json({ message: 'File uploaded', file: req.file });
});Note: multer limits are in bytes, not strings like express middleware.
Test that your endpoint now accepts larger payloads:
# Test with 10MB of JSON data
curl -X POST http://localhost:3000/api/data \
-H 'Content-Type: application/json' \
-d '{"largeArray": [' + Array(1000000).fill(1).join(',') + ']}'
# Or use Node to test programmatically
const data = { largeArray: new Array(100000).fill(1) };
fetch('http://localhost:3000/api/data', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
})
.then(res => res.json())
.then(data => console.log('Success:', data))
.catch(err => console.error('Error:', err));Security Considerations: While increasing limits enables larger payloads, set them appropriately for your use case. Unnecessarily large limits consume more server memory and make your application vulnerable to denial-of-service (DoS) attacks. A malicious client could send massive payloads to exhaust memory. Consider:
1. Set a reasonable maximum: If your app only needs 10MB payloads, don't set it to 500MB.
2. Use reverse proxies: Nginx and Apache can enforce limits before traffic reaches your Node app, protecting your application.
3. Rate limiting: Combine with rate limiting middleware to prevent abuse.
4. Monitoring: Log large requests to identify unusual patterns.
5. Validation: Always validate payload structure and sizes on the application side.
TypeScript with Express: If using TypeScript, the syntax is identical—body-parser types are included in express's type definitions.
Alternative Middleware: Some applications use the body-parser npm package directly instead of express's built-in version. The syntax is the same but be aware of version differences.
Error: Listener already called (once event already fired)
EventEmitter listener already called with once()
Error: EACCES: permission denied, open '/root/file.txt'
EACCES: permission denied
Error: Invalid encoding specified (stream encoding not supported)
How to fix Invalid encoding error in Node.js readable streams
Error: EINVAL: invalid argument, open
EINVAL: invalid argument, open
TypeError: readableLength must be a positive integer (stream config)
TypeError: readableLength must be a positive integer in Node.js streams