Cause 1 of 5
The default heap limit is smaller than the job needs.
Raise it for that one command: NODE_OPTIONS=--max-old-space-size=4096 npm run build (the value is in megabytes). Only ask for memory the machine or container actually has — requesting 8 GB inside a 2 GB container just moves the failure to the kernel's OOM killer, which gives you no stack trace at all.
Cause 2 of 5
A container memory limit is lower than what Node assumed.
Size the heap against the container, not the host: set --max-old-space-size to roughly 75% of the container's limit, leaving room for the rest of the process. Raise the container limit too if the workload genuinely needs it.
Cause 3 of 5
The code holds an entire dataset in memory at once.
Stream it. Read files and HTTP bodies through streams or async iterators and process in batches; a JSON.parse of a multi-gigabyte file cannot be made to fit by any flag. For line-based data, readline over a read stream is a small change.
Cause 4 of 5
There is a leak — something grows and is never released.
Take two heap snapshots minutes apart and compare: run with node --inspect, open chrome://inspect, and use the Memory panel. Listeners added per request, an unbounded cache Map, and closures captured by a module-level array are the usual finds. A MaxListenersExceededWarning in the logs is an early tell.
Cause 5 of 5
Parallel workers each claim a full heap.
Jest, webpack and similar spawn one worker per core, and the limit is per process. Cap them — jest --maxWorkers=2 — so the total stays inside the machine's RAM.