Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is Electron's app.getAppPath() pointing to?

I am using browserify to merge all the .js files of my app into a dist/main.js. My package.json looks like:

"main": "./dist/main.js",
"scripts": {
    "start": "electron ./dist/main.js",
},
"bin": {
  "electron": "./node_modules/.bin/electron"
}

and I can correctly run my application with npm run start. However if in main.js I use app.getAppPath() I get:

/home/myuser/projects/electronProject/node_modules/electron/dist/resources/default_app.asar

I would expect this to be

/home/myuser/projects/electronProject/dist/main.js

Did I misunderstood the usage of this method? How can I get the path of the Electron program entrypoint? What is the role of default_app.asar?

Thanks

like image 610
Marco Ancona Avatar asked Nov 09 '16 16:11

Marco Ancona


People also ask

Where do Electron apps install?

The installer will indeed install in %appdata% if you have set build/nsis/perMachine to false (which is default) in your package. json. If you set perMachine to true, the program will be installed in program files, and furthermore if you set oneClick to false, you allow the user to choose where to install.

How do I close the Electron app?

If the user pressed Cmd + Q , or the developer called app. quit() , Electron will first try to close all the windows and then emit the will-quit event, and in this case the window-all-closed event would not be emitted.


3 Answers

It returns the current application directory:

app.getAppPath()
Returns String - The current application directory.

From the docs.

An asar file is a simple archive format that just appends the files to each other. I'm not sure exactly how you're building the application but tools like electron-packager and electron-builder output the files into a resources/app.asar archive and run the files from there. That means that your current application directory is going to be something/resources/app.asar. From there your main file is located at something/resources/app.asar/main.js.

like image 138
DavidTheWin Avatar answered Sep 27 '22 19:09

DavidTheWin


Why aren't you using __dirname (node.js) or process.resourcesPath (electron)?

https://github.com/electron/electron/blob/master/docs/api/process.md https://nodejs.org/docs/latest/api/globals.html#globals_dirname

like image 30
fhå Avatar answered Sep 27 '22 20:09

fhå


For whom may ran into the same problem... It's maybe a problem with your electron configuration field main in package.json

The script specified by the main field is the startup script of your app, which will run the main process.

The example code from offical websites:

{
  "name": "your-app",
  "version": "0.1.0",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  }
}

app.getAppPath() output:

YOUR_PATH_TO/electron-quick-start

If you change the code snippet to

{
  "name": "your-app",
  "version": "0.1.0",
  "main": "main.js",
  "scripts": {
    "start": "electron YOUR_PATH_TO/main.js"
  }
}

Then app.getAppPath() output:

YOUR_PATH_TO/electron-quick-start/node_modules/electron/dist/resources/default_app.asar

So the consolution is : If you want to change the startup script, change it in the main field, not just change it in scritps field...

like image 25
han Avatar answered Sep 27 '22 20:09

han