I’m building and trying do deploying a packaged electron app. FOr the packaging i used
electron-packager
electron-installer-debian
electron-installer-dmg
electron-winstaller
and I’m facing a little issue where I have to store tha appa datas somewhere in my user computer.
I saw that the good practice is to use the the folder in the path that is returned by the electron method app.getPath('userData')
.
from the docs
It is The directory for storing the app’s configuration files, which by default it is the appData
directory appended with the app name.
%APPDATA% on Windows
$XDG_CONFIG_HOME or ~/.config on Linux
~/Library/Application Support on macOS
By my tests sometimes this folder is not created automatically when the app is installed and other times yes and I’m wondering if i should create it or not.
Right now i’m quitting the app if this folder isn’t present in the pc with the following code
var DatasPath = app.getPath('userData')
if (!fs.existsSync(DatasPath)){
process.exit()
}
So the question is
- should i create the
DatasPath
folder withfs.mkdirSync(DatasPath);
when it is not present or it is ‘bad practice to do so’, and if I can create the folder i have to warning the user the i have just added that folder?
3
Answers
(Expanding my reply from a “comment” to an “answer”)
It seems you are taking “userData” too literally? It is not an actual “folder” named “userData – it is a path to where the operating system stores data for that application.
Electron
currently runs on 3 operating systems and each one does things differently. For our convenience, Electron hides those differences by creating the wrapper method app.getPath(name) so the same code will work on each OS.Try this: put the line below in your
main.js
script:(the “*********” will be your user account name.)
UPDATED:
Run the code below in
main.js
and then look in the folder specified by the “userData” pathAt
pathConfig.js
In each operating system the appData folder has a different path and the perfect way of getting this path is by calling app.getPath(‘userData’) in the main process.
But there is a package that can handle this for you, it stores data in a JSON file and update it in every change.
In my opinion this package is much better than handling everything by your self.
Read more :
https://www.npmjs.com/package/electron-data-holder