debugjavascriptnodejsMajorpending
Debug: Node.js heap out of memory
Viewed 0 times
heap out of memorymax-old-space-sizememory limitheap snapshotv8
Error Messages
Problem
Node.js crashes with 'JavaScript heap out of memory' during build, test, or runtime.
Solution
Diagnose and fix heap memory issues:
Common causes:
- Fix: Set
- Fix: Use
- Fix: Use streaming JSON parser (JSONStream, stream-json)
- Fix: Process in batches, stream, or set size limits
- Fix: Use project references, incremental builds
- Fix:
# 1. Increase heap size (immediate fix)
node --max-old-space-size=4096 app.js # 4GB
# For npm scripts
NODE_OPTIONS='--max-old-space-size=4096' npm run build
# 2. Profile memory usage
node --inspect app.js
# Open chrome://inspect -> Memory tab -> Take heap snapshot
# 3. Programmatic monitoring
const v8 = require('v8');
setInterval(() => {
const stats = v8.getHeapStatistics();
const used = Math.round(stats.used_heap_size / 1024 / 1024);
const total = Math.round(stats.total_heap_size / 1024 / 1024);
console.log(`Heap: ${used}MB / ${total}MB`);
}, 10000);Common causes:
- Webpack/Vite builds: Large source maps or many modules
- Fix: Set
NODE_OPTIONS=--max-old-space-size=4096- Fix: Use
devtool: 'eval-source-map' for dev- Large JSON parsing:
JSON.parse()on huge files
- Fix: Use streaming JSON parser (JSONStream, stream-json)
- Accumulating data: Arrays/objects that grow without bounds
- Fix: Process in batches, stream, or set size limits
- TypeScript compilation: Large projects
- Fix: Use project references, incremental builds
- Jest tests: Leaking between test files
- Fix:
--maxWorkers=2, --forceExit, investigate leaking globalsWhy
Node.js default heap is ~1.5GB (v8 default). Build tools, large datasets, and memory leaks can easily exceed this.
Context
Node.js applications hitting memory limits
Revisions (0)
No revisions yet.