Is there any localStorage for electron?


#1

While running my electron desktop App, I want to store images to some localStorage of electron. Can someone plz help me?? Is there any NPM localStorage for electron?


#2

Not sure if you can directly access localStorage, but you can easily persist data without it – just store any data locally using regular node.js file methods. This would more suited for files than localStorage anyway. For JSON data can also use something like electron-json-storage.


#3

I have tried using electron-json-storage but could not find the location of the saving data… @lauris


#4

From the README file:

[…] write and read JSON objects to/from operating system application data directory, as defined by app.getPath(‘userData’)

If you are not sure where your operating system application data directory is located, you can just check the value of app.getPath(‘userData’), for example like this console.log(app.getPath(‘userData’))


#5

Thank you so muc it worked and i found the JSON file. :slight_smile:


#6

You can use the normal browser localStorage system. It is only accessible to the renderer process.


#7

EDIT : As pointed out by @idleberg. I’m wrong with ‘You can’t …’. I was confused by another similar issue.

You can’t. I’m sure you must also be getting an error in console. localStorage, IndexedDB and such do not work over file:// protocol.

Very glad that I made decision to serve my app inside webview. I get all the goodness of browser, plus I can inject any node API’s as needed.

And on top of that I can port minimal version of my app to web browsers any day with little modifications.

And the best of all, I don’t have to worry about delivering app updates. Any updates are applied using ServiceWorkers inside webview. Good for a rolling release model.


#8

I don’t think that’s correct. Atom stores data in both, localStorage and IndexedDB

I also checked other Electron apps (Visual Studio Code and Kap), and they all let you save data persistently in localStorage and IndexedDB.


#9

HTML5 localStorage works in Electron, I’m using a js library called “stash.js” to simplify the operation. Other libraries that utilize HTML5 localStorage should work too in most cases.

http://rezitech.github.io/stash/

Nothing special in main.js required

The example on the Github page worked fine