Node.js — RangeError: Invalid string length or buffer too large
Resolve range errors from oversized strings or buffers by streaming data instead of concatenating it in memory.
Symptoms
- RangeError: Invalid string length
- RangeError [ERR_BUFFER_TOO_LARGE]: Cannot create a Buffer larger than 4294967296 bytes
- It works for small inputs and fails for large ones
- Memory climbs sharply before the error
Possible causes
- Concatenating a stream into one giant string exceeds the maximum string length
- Allocating a buffer larger than the platform limit
- Reading a whole large file into memory at once
- A loop keeps appending to a string without limit
Diagnosis steps
Locate the accumulation
grep -rn "+=\|\.join(\|concat(" src | grep -i "data\|chunk\|buffer"Interpretation
Repeated string concatenation or joining is the usual pattern that grows memory unbounded.
Stream instead of buffering
node -e "require('fs').createReadStream('big.log').on('data',c=>{/* handle chunk */})"Interpretation
Processing chunk by chunk keeps memory flat and avoids exceeding string or buffer limits.
Measure input size first
du -h big.logInterpretation
Knowing the file size relative to the string and buffer limits tells you whether streaming is mandatory.
Root cause
The code built a single string or buffer that exceeded the runtime maximum, because it accumulated the entire input in memory rather than processing it in bounded chunks.
Fix
Stream the data and process chunks incrementally, write to a file or sink instead of concatenating, and enforce explicit size limits with clear errors for inputs that are genuinely too large.
Verify the fix
Process the large input again and confirm it completes with flat memory usage and no range error.
Symptom walkthrough
Processing a large input fails with RangeError: Invalid string length or ERR_BUFFER_TOO_LARGE, and memory usage rises steeply before the failure. The code accumulates everything into one string or buffer, which hits the engine maximum rather than any application limit. Small inputs work, which masks the pattern until real data arrives.
Fix in practice
Convert the accumulation into a streaming pipeline: read chunks, transform them, and write to a file, socket or database rather than concatenating. Where a hard limit is legitimate, check the size up front and reject with a descriptive error instead of letting the runtime crash.
Verification
Reprocess the same large input and confirm it finishes with flat memory and no range error, then check a representative result to be sure the streaming path produced correct data. A size guard at the entry point prevents the same failure from returning under even larger inputs.
Diagnostic commands
Check the behavior with commands like these:
grep -rn "+=\|\.join(\|concat(" src | grep -i "data\|chunk\|buffer"
node -e "require('fs').createReadStream('big.log').on('data',c=>{/* handle chunk */})"
du -h big.log