2

I am using electron-builder to package my electron app.
My app has a CLI module, so what I need is:

- After I finish installing the application (using allowToChangeInstallationDirectory: true), I need to add the install location to the PATH variable.

I can't find anything on this but this question Adding electron application path to user environment variables after install, which doesn't have a viable answer

Pavan Kumar
  • 4,182
  • 1
  • 30
  • 45
Alex
  • 81
  • 6

2 Answers2

5

I have made it work like this:

  • Download EnvVarUpdate.nsh
  • Save it in the same folder as installer.nsh
  • Add the following to your package.json:
{
  // ...
  "build": {
    // ...
    "nsis": {
      "warningsAsErrors": false
    }
  }
}
  • In installer.nsh add:
!include "EnvVarUpdate.nsh"
!macro customInstall
    ${EnvVarUpdate} $0 "PATH" "A" "HKLM" "$INSTDIR"
!macroend
!macro customUnInstall
    ${un.EnvVarUpdate} $0 "PATH" "R" "HKLM" "$INSTDIR"
!macroend

This updates the path variable with the instalation dir on install and deletes it on uninstall.

Kaspi
  • 3,538
  • 5
  • 24
  • 29
Alex
  • 81
  • 6
  • I get an error during the installation if I don't run it as Administrator on Windows 10: `Could not write updated PATH to HKLM` any ideas? On Windows 10 normal users have their own PATH variable which should be editable. – Kaspi Aug 04 '20 at 08:33
  • i have a package I'm creating with electron-installer but I don;t see installer.nsh anywhere... where is it supposed to be? – dWitty Nov 30 '20 at 10:45
0

Within electron-builder you can use a custom NSIS script, which will be executed during installation progress: Electron-Builder custom NSIS script

To detect the intallation directory you can use the param $INSTDIR in the NSIS custom script.

The NSIS documentation describe the PATH manipulation: NSIS PATH manipulation

See below an example for you custom NSIS script to append the install directory to the PATH (build/installer.nsh):

!macro preInit
   ${EnvVarUpdate} $0 "PATH" "A" "HKLM" "$INSTDIR"
!macroend
Paul Franke
  • 579
  • 3
  • 11
  • I did this but i get this error: `Invalid command: "${EnvVarUpdate}" Error in macro preInit on macroline 1` – Alex Oct 30 '19 at 07:14
  • I fixed it. Just that i had to download `EnvVarUpdate.nsh` first and i used `!macro customInstall` instead of `!macro preInit`(this didnt work) – Alex Oct 30 '19 at 08:09