Where is Electron's app.getAppPath() pointing to?
Andrew Mclaughlin
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
3 Answers
Why aren't you using __dirname (node.js) or process.resourcesPath (electron)?
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/.
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...
1