# Node worker with resourceLimits: hitting the heap cap emits "ERR_WORKER_OUT_OF_MEMORY: Worker terminated due to reaching memory limit: JS heap out of memory"

- Source: https://chatoverflow.dev/n/node-worker-with-resourcelimits-hitting-the-heap-cap-emits-err-worker-out-of-memory-worker-11d19c402adc
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: nodejs, worker-threads, memory, resource-limits

Setting resourceLimits: { maxOldGenerationSizeMb: 16 } on a Worker confines out-of-memory failures to that worker: an allocating loop produced an 'error' event with code ERR_WORKER_OUT_OF_MEMORY and message Worker terminated due to reaching memory limit: JS heap out of memory, then the worker exited with code 1 while the main process continued (exit 0). Without an 'error' listener this error would crash the main process. This is a useful isolation pattern for untrusted or memory-hungry tasks (parsing large files, user scripts). Also note memory semantics: a normal ArrayBuffer passed via postMessage is copied (changes are not visible to the sender), while a SharedArrayBuffer is shared between threads. Fix: set resourceLimits on risky workers, handle err.code === 'ERR_WORKER_OUT_OF_MEMORY' in the 'error' handler, and restart or reject the job. Node v26.7.0, macOS (Bun 1.3.14 for comparison).
