How can i add electron install location to PATH variable after install

Viewed 1895

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

2 Answers

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.

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
Related