95

I can't seem to locate a built in mechanism to store user settings. I was hoping that electron provided a standard method for storing user settings across all desktop platforms. If there isn't a precedent for this I can implement it myself, I just didn't want to jump to a custom solution immediately. Research online is pretty sparse in this area. Thanks!

AsukaMinato
  • 1,017
  • 12
  • 21
CodeManiak
  • 1,903
  • 4
  • 19
  • 32
  • 1
    I stored my settings using localStorage but I'm very interested to know more about how this works with Electron. Since it's messy to clear localStorage and a reinstall of a Electron app does not seem to clear it. – E. Sundin May 30 '15 at 10:56

6 Answers6

85

Each platform has different default locations for different kinds of data. So, if you want to store data in default locations based on platform, check out app.getPath(name)

It retrieves a path to a special directory or file associated with name.

You can also use it to differentiate between data the user wants to save, and data your application saves that you don't want to clutter up users directories.

Or if you just want to store files reletive to a specific path you can use the app.setPath(name,path)

vicke4
  • 2,973
  • 1
  • 20
  • 20
Josh
  • 3,225
  • 25
  • 22
48

I've faced this particular problem with my Electron app and this post inspired me to write an NPM module called electron-json-storage.

This module allows to easily write/read JSON to/from app.getPath('userData'):

const storage = require('electron-json-storage');

// Write
storage.set('foobar', { foo: 'bar' }).then(function() {

    // Read
    storage.get('foobar').then(function(object) {
        console.log(object.foo);
        // will print "bar"
    });

});
jviotti
  • 17,881
  • 26
  • 89
  • 148
  • `electron-json-storage` is awesome, another choice is `lowdb` – JUO Jul 26 '16 at 10:19
  • 9
    Just curious but what's the advantage of electron-json-storage vs just `var someObj = JSON.parse(fs.readFileSync(path, {encoding: "utf8"}))` and `fs.writeFileSync(path, JSON.stringify(someObj)});` Even making it async would not add more than a few lines. – gman Nov 23 '16 at 21:57
  • 3
    Also look at [electron-settings](https://github.com/nathanbuchar/electron-settings). – k7sleeper Dec 22 '16 at 08:20
  • Great resource. Although, it looks like the library doesn't support promises natively like you have in the example. – Keith Holliday Sep 03 '17 at 17:50
  • @gman The storage modules implement convenience functions that you'd likely end up implementing yourself, keep the code DRY, separate concerns, test units, track issues, prevent code rot, etc. – None Oct 30 '19 at 20:22
  • There are a bunch of module options. [electron-store](https://www.npmjs.com/package/electron-store) looks to be pulling away from the others in [npm trends](https://www.npmtrends.com/electron-config-vs-electron-json-storage-vs-electron-settings-vs-electron-store). – None Oct 30 '19 at 20:29
  • 2
    Every dependency you add is a larger surface area for exploits, yet another piece of code that has to updated constantly, and if you're a professional developer you also need to audit the code. It's arguably bad practice to take an entire bloated library just to replace 2 lines of code. – gman Oct 31 '19 at 03:04
  • Just to answer questions why a library here is useful, it's not just about getting the job done, it's also about not worrying about the implementation behind the `storage.set` or `get` – windmaomao Oct 31 '21 at 20:45
21

Electron doesn't give you anything out of the box for this. However, Electron does give you a method for getting the idiomatic location of storing user data in a cross platform way via the app.getPath API.

I'd say the 3 most common ways to do this are:

  • localStorage (or any HTML5 storage API)
  • flat JSON file (this is what I do, and I use electron-store for it)
  • embedded database like IndexedDB, neDB, or sqlite

Which one you choose will depend on your app's needs. If you only need to access this data in the renderer process, then I'd just use localStorage. Most of the time it seems you need to access the data in both the main and renderer, so a JSON file makes sense. If you're dealing with lots of data or complex querying, then maybe a database makes sense. I wrote about this more in detail here.

Alex
  • 59,571
  • 22
  • 137
  • 126
ccnokes
  • 6,885
  • 5
  • 47
  • 54
5

How about LocalStorage? If you need to access these settings from the browser process, you probably need to write your own (or just use a node.js library that implements this)

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
1

The best way that I have found is to store it in a simple file as JSON. The problem is that if you store that JSON in the app dir, then when you update the app, it will get wiped out. So you want to put it in the default directory for user settings for the current operating system. LUCKILY!!!!! There is a library for node developers that will help you find the userdata directory. The module is called appdirectory, and I have used it several times. It is extremely easy to use.

See APPDIRECTORY HERE

frosty
  • 21,036
  • 7
  • 52
  • 74
-1

One could store data in cookies; Electron has a mechanism for it (https://electronjs.org/docs/api/cookies) and the cookies can be retrieved in the browser (Angular: https://docs.angularjs.org/api/ngCookies/service/$cookies, React/Other: https://github.com/reactivestack/cookies)

I was able to get it working with Angularjs.