Angular "JavaScript heap out of memory" during ng build in CI
A large Angular production build (AOT plus optimization) can allocate more memory than Node's default old-space limit, and V8 aborts with a heap out-of-memory fatal error. Raising the Node heap via NODE_OPTIONS usually fixes it.
What this error means
ng build --configuration production dies with "FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory" and a V8 stack trace.
<--- Last few GCs --->
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memoryCommon causes
A large build exceeds the default Node heap
AOT compilation and optimization of a big app can need more than Node's default old-space size, so V8 runs out of heap.
A runner with limited memory
A smaller runner has less RAM, so the same build that passes on a larger machine hits the heap ceiling in CI.
How to fix it
Raise the Node heap with NODE_OPTIONS
Set --max-old-space-size high enough for the build, within the runner's available memory.
env:
NODE_OPTIONS: --max-old-space-size=4096
run: npx ng build --configuration productionReduce build memory pressure
Split large lazy modules and use a runner with more RAM if the heap flag alone is not enough.
How to prevent it
- Set NODE_OPTIONS max-old-space-size for large Angular builds in CI.
- Keep the value below the runner's physical memory.
- Use a runner sized for your build, and lazy load to shrink it.