Simple encrypted data persistence for your Electron app - Save and load user settings, app state, cache, and more.
You can use it as a single store for both the main and renderer processes.
It has the same features as the well-known electron-store library, but solves several other things:
- Encrypts the data saved on disk (using Electron's built-in
safeStorageAPI) - Support for React hooks (auto re-renders view on store update)
- Works in the
rendererprocess (unlikeelectron-store- issue link) - Has CommonJS exports (
electron-storehas only ESM exports) - Uses the same API as
zustandfor getting values from store (viauseEncryptedStoreReact hook)- e.g.
const encrypted = useEncryptedStore(store => store.encrypted)
- e.g.
npm i encrypted-electron-storepnpm i encrypted-electron-storeyarn add encrypted-electron-storeimport EncryptedStore from 'encrypted-electron-store/main'
const store = EncryptedStore.create<any>()
// or: const store = EncryptedStore.create<{ encrypted: string }>()
// or: const store = EncryptedStore.create<IStore>()
store.set('encrypted', 'π')
console.log(store.get('encrypted'))
//=> 'π'
store.delete('encrypted')
console.log(store.get('encrypted'))
//=> undefined
// IMPORTANT: If you want to use the store in the renderer process
window.webContents.on('did-finish-load', () => {
encryptedStore.setBrowserWindow(window)
})If you want to use this library in the renderer process, you first need to call this function in the preload.ts/js file.
// preload.ts/js
import { preloadEncryptedStore } from 'encrypted-electron-store/preload'
preloadEncryptedStore()
// ... rest of the fileAdd the EncryptedStoreProvider component in main.tsx/jsx or wherever you want in your React project.
// main.tsx/jsx
import { EncryptedStoreProvider } from 'encrypted-electron-store/react'
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<EncryptedStoreProvider>
<App />
</EncryptedStoreProvider>
</React.StrictMode>
);Use the useEncryptedStore() hook wherever you want in your React project.
// e.g. App.tsx/jsx
import { useEncryptedStore } from 'encrypted-electron-store/react'
function App() {
const { store, setStore } = useEncryptedStore<{ encrypted: string }>()
const encrypted = useEncryptedStore<{ encrypted: string }>((store) => store.encrypted);
setStore({ encrypted: 'π' });
return (
/* Gets automatically re-rendered when the value changes */
<p>{encrypted}</p>
)
}import EncryptedStore from 'encrypted-electron-store/vanilla'
// You need to use `await` because the library reads the initial store from the file on disk.
const store = await EncryptedStore.create<{ encrypted: string }>()
store.set('encrypted', 'π')
console.log(store.get('encrypted'))
// => 'π'You can find examples of usage in real projects in the examples folder.
todo: Here will be specified detailed documentation; or just link to the official docs
- Clone this repo:
git clone https://github.com/rixcian/encrypted-electron-store- Make sure you have node v22:
nvm use- Install dependencies:
npm i- After making changes, run tests:
npm run test- Create changelog:
npx @changesets/cli- As a maintainer, after review, I will run:
npx @changesets/cli version- Build the library:
npm run build- And publish it to NPM with:
npx @changesets/cli publish| Feature | encrypted-electron-store | electron-store |
|---|---|---|
| Works in main & renderer processes | β Yes | π‘ Only in main process |
| ESM & CommonJS exports | β ESM & CJS Supported | β Only ESM exports supported |
| Encryption | β Uses Electron's built-in encryption | π‘ Not sufficient (encryption password in plaintext) |
| React Integration | β Built-in React hooks | β No React integration |
| Vanilla JS Integration | β Simple API | β Simple API |
| File Extensions | β Configurable | β Configurable |
| Works with files atomically | β Yes | β Yes |
| JSON Schema validation | π‘ Work in progress | β Yes |
| Migrations | β Yes, if there'll be demand | π‘ Yes, with bugs (more info) |
- Finish basic React implementation
- Make
const time = useEncryptedStore((store, setStore) => store.time)architecture possible (similar tozustand)
// Usage
const { store, setStore } = useEncryptedStore<Store>()
const time = useEncryptedStore<Store>((store) => store.time)
const setStore = useEncryptedStore<Store>((_, setStore) => setStore)
// Another possible usage
const { store, setStore } = useEncryptedStore<Store>()
const { time, setStore } = useEncryptedStore<Store>((store, setStore) => ({
time: store.time,
setStore,
}))