Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use electron's app.getPath() to store data?

I want to store images on the users computer, so I figure it should be stored in users data folder, as described here.

app.getPath(name)

name. Returns String - A path to a special directory or file associated with name. On failure an Error is thrown. You can request the following paths by the name:

  • home User's home directory
  • appData Per-user application data directory, which by default points to:

    %APPDATA% on Windows $XDG_CONFIG_HOME or ~/.config on Linux ~/Library/Application Support on macOS

  • userData The directory for storing your app's configuration files, which by default it is the appData directory appended with your app's name.

  • ...

This is what I think you're supposed to do:

const app = require('electron'); alert(app.getPath('userData')); 

But I get "getPath is not a function". I am not sure where to put it. It does not work from my html file or the renderer file, and I'm not sure how to use it from the main file because that's not linked to the web page.

like image 526
stackers Avatar asked Dec 24 '17 01:12

stackers


People also ask

How can you tell if an app is an electron?

If you see an app. asar file, or something similar with the . asar suffix, it is most likely an Electron App. Windows: Open up the program files directory of the application you are wondering about, and check the file folder for any file with .

How is local storage used in electrons?

Electron doesn't have the strict limitation of data flow. You can store the data in localStorage, or store the data in main thread and expose it to global. Or, you can also use electron-store for simple key-value storage. When the application grows bigger and bigger, the data flow will become harder to maintain.


1 Answers

Since the remote method is being considered deprecated, as shown here, I'd suggest you do this:

const {app} = require('electron'); console.log(app.getPath('userData')); 
like image 177
stackers Avatar answered Sep 19 '22 14:09

stackers