6

I'm currently working on an Electron app which uses a third party software. At some point I'm checking if that software is installed on the user's computer (it has to be in PATH), and if it's not there I just run the installation. As I said, the installation will append a directory to the PATH variable. After this happens, I need to restart the app in order to have access to the updated variables.

I already tried to use relaunch, just as in the docs, but it doesn't refresh the variables:

app.relaunch()
app.exit(0)

If I restart the app manually, then everything works ok.

Does anyone have some ideas ? Thanks.

Alin
  • 420
  • 1
  • 5
  • 17
  • 1
    Hi @Alin, did you find a solution for this issue ? I am facing the same issue. – GeoAstronaute Jan 08 '19 at 11:58
  • Hi, I couldn't find a solution to update the variables, but now I'm just not using the environment variables anymore...I use the entire path of the file...and that's better for me. – Alin Jan 17 '19 at 08:23
  • While waiting for a better solution, I will ask my users a manual restart of the program under Windows "For the changes to take effect" with a close button. I wonder how you discover the full path of your third-party library? I tried using the `which` package, but it uses` process.env.PATH` to discover the paths. – GeoAstronaute Jan 18 '19 at 09:20
  • In my case I already know the path for Windows and I just hardcoded it in the code. – Alin Jan 19 '19 at 10:12

1 Answers1

1

My solution just works for production:

In production, your application could not get your PC's environment variable. So, when you create mainWindow in main.js, you should read environment variables and pass it to process.env variable. After you relaunch the application, it will reload env again as you expected.

Library in use:

https://github.com/sindresorhus/shell-env

https://github.com/sindresorhus/shell-path

import shellEnv from 'shell-env';
import os from 'os';
import shellPath from 'shell-path';

const isDevelopment = process.env.NODE_ENV === 'development';

function createMainWindow() {
  mainWindow = new BrowserWindow({
    webPreferences: {
      nodeIntegration: true,
    },
    width: 800,
    height: 1000,
  });

  // ...

  if (!isDevelopment) {
    // TODO: if we're running from the app package, we won't have access to env vars
    // normally loaded in a shell, so work around with the shell-env module
    // TODO: get current os shell
    const { shell } = os.userInfo();

    const decoratedEnv = shellEnv.sync(shell);

    process.env = { ...process.env, ...decoratedEnv };

    console.log('DEBUG process.env', process.env);

    // TODO: If we're running from the app package, we won't have access to env variable or PATH
    // TODO: So we need to add shell-path to resolve problem
    shellPath.sync();
  }

  // ...
}

After relaunching the application, the environment variables will be updated.

app.relaunch()
app.exit(0)

Notice: Without check !isDevelopment, after relaunching, the application won't be displayed. I have no idea.

Long Nguyen
  • 9,898
  • 5
  • 53
  • 52