Mat.C
Mat.C

Reputation: 1429

Electron store my app datas in 'userData' path

I'm building and trying do deploying a packaged electron app. FOr the packaging i used

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

Upvotes: 1

Views: 10894

Answers (4)

satolas
satolas

Reputation: 179

Just to clarify : Actually appdata/yourApp or Application Support/yourApp etc..

Will be created for sure because it is holding a lot of files and folders used by Chromium and ElectronJs to work. Like caches, cookies etc...

/Users/home/Library/Application Support/MyApp :

Upvotes: 0

Abde Laziz
Abde Laziz

Reputation: 139

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

Upvotes: 0

tpikachu
tpikachu

Reputation: 4854

At pathConfig.js

function getAppDataPath() {
  switch (process.platform) {
    case "darwin": {
      return path.join(process.env.HOME, "Library", "Application Support", "myApp");
    }
    case "win32": {
      return path.join(process.env.APPDATA, "myApp");
    }
    case "linux": {
      return path.join(process.env.HOME, ".myApp");
    }
    default: {
      console.log("Unsupported platform!");
      process.exit(1);
    }
  }
}

const appPath = __dirname;
const appDataPath =
  !process.env.NODE_ENV || process.env.NODE_ENV === "production"
    ? getAppDataPath() // Live Mode
    : path.join(appPath, "AppData"); // Dev Mode

if (!fs.existsSync(appDataPath)) {
    // If the AppData dir doesn't exist at expected Path. Then Create
    // Maybe the case when the user runs the app first.
    fs.mkdirSync(appDataPath);
}

Upvotes: 2

spring
spring

Reputation: 18487

(Expanding my reply from a "comment" to an "answer")

i don't know if i'm supposed to create it or not so i automatically make the app quit if there is not that folder

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:

console.log(app.getPath('userData'));

/Users/*********/Library/Application Support/MyCoolApp

(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" path

const fs = require("fs");
const path = require('path');
var datasPath = app.getPath('userData')
var data = "I am the cheese"
var filePath = path.join(datasPath, "savedData.txt")
fs.writeFileSync(filePath, data)

Upvotes: 6

Related Questions