Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron.js CLI arguments are not passed to node.js with js-flags

I am creating an app packaged/wrapped via electron.js consisting of a node.js part and some services, and a react frontend.

  • Platform: Windows 10 prof.
  • Electron version: 19.0.0
  • node.js (embedded): 16.14.2
  • chromium (embedded): 102.0.5005.61

I am trying to make the application start with more heap space (more RAM memory assigned). But as far as goes, all efforts are not working.

Options I tried:

  • passing it as command line arguments with build electron app (e.g. myapp.exe --max-old-space-size=4096 or also --js-flags='--max-old-space-size=4096' )
  • passing it via the index.js file that creates the window (e.g. app.commandLine.appendSwitch('js-flags', '--max-old-space-size=4096'); )
  • passing it directly via the process.env (process.env["max-old-space-size]=4096)

Everything did not seem to have any effect whatsoever ! (References: https://github.com/electron/electron/issues/2056, https://warpdesign.fr/electron-development-how-to-configure-nodejs-and-runtime-chrome-flags https://www.geeksforgeeks.org/command-line-arguments-in-electronjs/ )

The memory keeps being fixed to 2 GB ( output MAX HEAP SIZE (bytes): 2132773076 ~ 1.99 GB ) Also, when outputting what is in the process.env, there is a NODE_OPTIONS: '--max-old-space-size=4096' ... so in some way it is picked up ??? But never applied to the embedded node.js.

How can I force this to increase it heap space ? I tried all suggestions. Is that broken in electron ? or am I still doing something wrong ?

like image 454
Peter De Leuze Avatar asked Mar 15 '26 00:03

Peter De Leuze


2 Answers

These steps will help you out, please try them.

  1. app.commandLine.appendSwitch
    1. This method should be called before the Electron app is ready,This ensures that the setting is applied before any windows are created or any other processes start.
     const { app } = require('electron');
     app.commandLine.appendSwitch('js-flags', '--max-old-space-size=4096');
     app.on('ready', () => {});
    
  2. Try setting env variable in shell
    1. NODE_OPTIONS="--max-old-space-size=4096" electron .
  3. Verify
    1. console.log(require('v8').getHeapStatistics().total_available_size);
like image 94
Shahed Avatar answered Mar 16 '26 12:03

Shahed


The max-old-space-size configuration you are trying to set needs to be set before the process initializes itself. By using the command line flags or trying to set the env variable while the process is already running, the process has already passed the stage where setting the space size can occur.

As per the docs (which explicitly use --max-old-space-size in the example):

Certain Electron behaviors are controlled by environment variables because they are initialized earlier than the command line flags and the app's code.

So your only option to is figure out a way to set the environment variable for your electron executable after packaging. It's possible that whichever packaging option you're using (electron-forge, electron-builder, etc) has this configuration, but you'd have to comb the related documentation for it (a cursory glance by me didn't reveal what you need).

How you achieve this will entirely depend on your use case, one option if you are installing on a linux system and intend to launch from terminal, is using your install script to set up an alias in the user's profile like

alias myapp='NODE_OPTIONS="--max-old-space-size=4096" /usr/local/bin/myapp.electron'

A similar approach (prepending the env variable to the executable call) would work for setting up a desktop icon.

like image 21
leitning Avatar answered Mar 16 '26 14:03

leitning



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!