electron-webpack icon indicating copy to clipboard operation
electron-webpack copied to clipboard

getAppPath returns default_app.asar

Open HaNdTriX opened this issue 6 years ago • 4 comments

Code

console.log(require('electron').app.getAppPath());

Expected

Should return the current application directory.

Current behaviour

It returns:

/Users/.../appDir/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar

HaNdTriX avatar Dec 08 '18 15:12 HaNdTriX

I believe this happens, because we start electron with a javascript file path instead of the root directory path of the package.json:

Related code

https://github.com/electron-userland/electron-webpack/blob/ae2a2f1ab3f2671831eb08169ee1d20aa00dfbb6/packages/electron-webpack/src/dev/dev-runner.ts#L63

Should be:

args.push(projectDir)

Related Issues

  • #242
  • #239

HaNdTriX avatar Dec 08 '18 15:12 HaNdTriX

Alternative

Implement the following electron code:

...
  if (packageJson.version) {
    app.setVersion(packageJson.version)
  }
  if (packageJson.productName) {
    app.setName(packageJson.productName)
  } else if (packageJson.name) {
    app.setName(packageJson.name)
  }
  app.setPath('userData', path.join(app.getPath('appData'), app.getName()))
  app.setPath('userCache', path.join(app.getPath('cache'), app.getName()))
  app.setAppPath(packagePath)
...

https://github.com/electron/electron/blob/c8030a0f2aaf299f823f8a7dc68e29e69abb102f/default_app/main.js#L266-L276

HaNdTriX avatar Dec 08 '18 15:12 HaNdTriX

I believe this happens, because we start electron with a javascript file path instead of the root directory path of the package.json:

Should be:

args.push(projectDir)

I also experienced this issue, patching the code as suggested works and I get the right userData name.

As you removed dist/main/main.js from the code, electron will not work unless you will add to the package.json of the application "main": "dist/main/main.js"

I did it manually in my project but I think before creating a PR we should figure how to make it work without relying on manual adjustments of the package.json. @HaNdTriX wdyt?

esakal avatar Aug 09 '19 13:08 esakal

We could check if package.json has a main field starting with dist/; if so, pass projectDir, and if not, pass the old path.join(projectDir, "dist/main/main.js") for compatibility (and perhaps issue a warning). That way, at least anyone who experiences this problem has the ability to correct it by adding the main field.

andersk avatar Nov 24 '20 02:11 andersk