Programming

Node.js Express process exits silently when parsing very large JSON payloads — signs of V8 heap exhaustion

Environment

Environment

Node.js 18.x running Express 4 on a 16‑GB RAM server under PM2; POST /import endpoint accepts JSON arrays produced by client tools. Some clients upload single JSON payloads hundreds of megabytes to multiple GB.

Symptoms

Symptoms

When a client posts a very large JSON payload, the Node process exits abruptly without an application stack trace; process manager shows exit code 1 and immediate restart. Smaller payloads succeed. No explicit 'out of memory' message written by application code.

Observations

Observations

Monitoring shows process memory climbs rapidly toward the V8 heap limit and CPU spikes from GC before the crash. Running Node with --trace-gc shows prolonged GC activity prior to exit. Replacing JSON.parse with a streaming JSON parser reduces memory usage and prevents crashes for the same test payload. Logs show the default bodyParser in Express buffered the entire payload into memory.

Already attempted

Already attempted

Increased --max-old-space-size to 8192, increased Express bodyParser limit to allow larger bodies, implemented a streaming parser (stream-json) in a test route which successfully processed the large payload, configured reverse proxy (nginx) client_max_body_size higher to avoid proxy rejection. However, simply increasing memory still risks OOM under concurrent uploads.

Question

Question

What are safe server-side patterns to handle very large JSON uploads in Node.js/Express to avoid process crashes, and which immediate mitigations should I apply to protect the server while implementing a streaming solution?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 1w ago• Trouble ID

Most likely causes

Synchronous JSON.parse or bodyParser buffering entire request into memory causing V8 heap exhaustion. 100%
Multiple concurrent large uploads exhausting aggregate memory even if single upload fits. 85%
Insufficient request size limits allowing malicious or accidental oversized payloads. 85%

Summary

Crashes result from parsing or buffering an entire large JSON payload in process memory, causing V8 heap exhaustion and process termination. Node/Express default body parsers accumulate bodies in RAM; increasing heap size is a stopgap that does not solve concurrency or malicious large uploads. The robust approach is to avoid fully buffering large payloads: stream the request, parse incrementally, or use a different upload format (ndjson or multipart) so that records can be processed and flushed without retaining the entire dataset in memory.

Suggested Fixes

Diagnostic step 1
Immediately enforce strict request size limits: set Express json/bodyParser limit to a conservative size and configure your reverse proxy (nginx 'client_max_body_size') to the same limit to reject oversized requests early.
Diagnostic step 2
Implement streaming parsing for large JSON payloads using libraries such as stream-json or JSONStream — these parse the input token by token without keeping the whole document in memory.
Diagnostic step 3
If input can be chunked, switch clients to send newline-delimited JSON (ndjson) or multipart uploads where each record is processed and persisted before reading the next; this greatly reduces memory pressure.

Continue troubleshooting

Matched from the title, description and tags