The OOM killed error occurs when npm or Node.js exhausts available memory during operations like install or build. Increase memory limits or optimize the operation.
Fixes FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
# Via environment variable
export NODE_OPTIONS="--max-old-space-size=8192"
# For specific command
node --max-old-space-size=8192 script.js
# In npm script
"build": "node --max-old-space-size=4096 ./node_modules/.bin/webpack"# Via environment variableexport NODE_OPTIONS="--max-old-space-size=8192"# For specific commandnode --max-old-space-size=8192 script.js# In npm script"build": "node --max-old-space-size=4096 ./node_modules/.bin/webpack"FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
OOM (Out of Memory) errors occur when Node.js's V8 JavaScript engine cannot allocate more memory for the heap. By default, Node.js has a memory limit (around 1.4GB on 64-bit systems) that can be exceeded during memory-intensive operations. This is different from system OOM (exit code 137) - this is Node.js's internal heap limit being exceeded before the OS kills the process.
Set higher memory limit:
# Via environment variable
export NODE_OPTIONS="--max-old-space-size=8192"
# For specific command
node --max-old-space-size=8192 script.js
# In npm script
"build": "node --max-old-space-size=4096 ./node_modules/.bin/webpack"Value is in MB.
More memory-efficient:
npm cinpm ci skips certain resolution steps that consume memory.
Free up resources:
npm cache clean --force
rm -rf node_modules
npm installMonitor memory during build:
# Watch memory usage
while true; do
ps aux | grep node | awk '{print $6/1024 " MB"}'
sleep 1
doneIf memory grows unbounded, there may be a leak.
Reduce memory usage:
// webpack.config.js
module.exports = {
optimization: {
splitChunks: { chunks: 'all' },
usedExports: true
},
// Disable source maps in production
devtool: false
};Ensure you're using 64-bit:
node -p "process.arch"
# Should show "x64" for 64-bit
# 32-bit has much lower memory limitsThe default heap limit varies by Node.js version and system architecture. Use --expose-gc and global.gc() for manual garbage collection in memory-critical operations. Consider using Worker threads for memory-intensive parallel operations. For builds, incremental compilation and caching significantly reduce memory pressure. Monitor with --inspect and Chrome DevTools for memory profiling.