Where to store user settings in Electron (Atom Shell) Application?

Electron

Electron Problem Overview


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!

Electron Solutions


Solution 1 - Electron

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)

Solution 2 - Electron

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"
    });

});

Solution 3 - Electron

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.

Solution 4 - Electron

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)

Solution 5 - Electron

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

Solution 6 - Electron

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCodeManiakView Question on Stackoverflow
Solution 1 - ElectronJoshView Answer on Stackoverflow
Solution 2 - ElectronjviottiView Answer on Stackoverflow
Solution 3 - ElectronccnokesView Answer on Stackoverflow
Solution 4 - ElectronAna BettsView Answer on Stackoverflow
Solution 5 - ElectronfrostyView Answer on Stackoverflow
Solution 6 - Electronuser6470694View Answer on Stackoverflow