Preload script path from the renderer process


#1

I’m having an issue while specifying the preload script path (within my webPreferences) while opening the BrowserWindow from the renderer process.

  • My __dirname from the main process points to the root directory of where my app lives (i.e. where i run npm from).

/Users/my-username/MyElectronApp/

  • Somehow, __dirname from a renderer process points to “/” (i.e. root of my machine!!) This is a problem because while specifying the preload script path i end up having to give the full absolute path!

/

  • I tried using remote.app.getAppPath() but end up getting a path that looks like this

/Users/my-username/MyElectronApp/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar

Few questions around this:-

  1. Firstly how do i get my app root directory from within the renderer process?
  2. Whats the best practice around this?