Cannot pass Node.js CLI args to cluster workers when using --instances
Problem
With moleculer-runner --instances N, Moleculer forks workers via Node cluster, but there is no way to pass Node.js/V8 arguments only to those workers.
Examples people need:
--max-old-space-size=768--max-old-space-size-percentage=25--trace-warnings
NODE_OPTIONS is a poor workaround because it also applies to the primary process (and anything else that inherits the environment).
This is especially painful in containers: each worker needs a controlled heap budget, while the primary is only a supervisor and should stay light.
Proposal
Add Moleculer Runner flags for cluster mode:
# 1) Pass any Node/V8 args to workers only
moleculer-runner -i 3 \
--worker-node-args="--max-old-space-size=768 --trace-warnings" \
services
# same via env
MOLECULER_WORKER_NODE_OPTIONS="--max-old-space-size=768" \
moleculer-runner -i 3 services
# 2) Optional: reserve memory for primary, split the rest equally across workers
moleculer-runner -i 3 --auto-worker-heap-reserve=1024 services
# e.g. 4096 MB available, reserve 1024, 3 workers
# → each worker gets --max-old-space-size=1024Implementation idea: apply worker args via cluster.setupPrimary({ execArgv }), so they affect forked workers only, not the primary.
When --auto-worker-heap-reserve is set, detect available memory (process.constrainedMemory() / cgroup, otherwise os.totalmem()), subtract the reserve, and inject equal --max-old-space-size per worker.
Notes
- With
--instances, the primary does not start a broker — it only forks/restarts workers --max-old-space-sizeis a V8 heap limit, not a hard RSS / cgroup / container memory limit- If you set
--max-old-space-sizeequal to the full container memory on each worker, every instance may try to grow toward that limit. With-i Nthat means up to roughlyN × container_limitof heap budget → classic container OOMKilled - Same trap with
--max-old-space-size-percentage: each worker calculates the percentage of the whole container, not1/N - So heap must be sized per worker (manually via
--worker-node-args, or automatically via--auto-worker-heap-reserve) - Related PR: https://github.com/moleculerjs/moleculer/pull/1364
Source: moleculerjs/moleculer