The ENOMEM error occurs when npm or Node.js runs out of available memory. This often happens with large projects, complex dependency trees, or systems with limited RAM.
Fixes npm ERR! ENOMEM: not enough memory
# Set via environment variable
export NODE_OPTIONS="--max-old-space-size=4096"
npm install
# Or inline
NODE_OPTIONS="--max-old-space-size=4096" npm install# Set via environment variableexport NODE_OPTIONS="--max-old-space-size=4096"npm install# Or inlineNODE_OPTIONS="--max-old-space-size=4096" npm installnpm ERR! ENOMEM: not enough memory
ENOMEM indicates the operating system denied a memory allocation request. This can happen when npm's JavaScript heap fills up, when the system is out of RAM, or when memory limits are imposed (containers, cgroups). npm operations involving many packages, particularly npm install on large projects, can consume significant memory for dependency resolution and file operations.
Give Node more heap space:
# Set via environment variable
export NODE_OPTIONS="--max-old-space-size=4096"
npm install
# Or inline
NODE_OPTIONS="--max-old-space-size=4096" npm installValue is in MB (4096 = 4GB).
Verify available RAM:
free -h
# Check what's using memory
top -o %MEMFree up memory:
1. Close browser tabs
2. Stop development servers
3. Close IDEs
4. Stop Docker containers not in use
npm ci uses less memory than npm install:
npm ciIt skips dependency resolution when package-lock.json exists.
Install in smaller batches:
# Install production deps first
npm install --omit=dev
# Then dev dependencies
npm installFor Docker/CI environments:
# Docker Compose
services:
app:
deploy:
resources:
limits:
memory: 4G
# GitHub Actions
jobs:
build:
runs-on: ubuntu-latest-16-coresLarge monorepos may need 8GB+ for npm operations. Consider using pnpm which is more memory-efficient. For constrained environments, pre-install dependencies in CI and cache node_modules. The --max-old-space-size flag affects Node.js heap; system RAM must still be available. Monitor memory usage during installs to find the right limit.