From ec2bf1a95d6c45c093f68df832bc6bcbdd372a5a Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Wed, 20 Apr 2022 16:59:30 +0200 Subject: [PATCH] Fix out of memory error when starting on system with low RAM. #12034 --- index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index e47093a3..b4511a85 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ const semver = require('semver'); const engines = require('./package.json').engines; const indexJsRestart = 'indexjs.restart'; const fs = require('fs'); +const os = require('os'); const path = require('path'); const {exec} = require('child_process'); const rimraf = require('rimraf'); @@ -44,7 +45,15 @@ async function build(reason) { return new Promise((resolve, reject) => { process.stdout.write(`Building Zigbee2MQTT... (${reason})`); rimraf.sync('dist'); - exec('npm run build', {cwd: __dirname}, async (err, stdout, stderr) => { + const env = {...process.env}; + const _600mb = 629145600; + if (_600mb > os.totalmem()) { + // Prevent OOM on tsc compile for system with low memory + // https://github.com/Koenkk/zigbee2mqtt/issues/12034 + env.NODE_OPTIONS = '--max_old_space_size=256'; + } + + exec('npm run build', {env, cwd: __dirname}, async (err, stdout, stderr) => { if (err) { process.stdout.write(', failed\n'); if (err.code === 134) {