const { app } = require('electron')
const fs = require('fs')
const path = require('path')
const filePath = path.join(app.getPath('userData'), 'data.json')
function saveData(data) {
const text = JSON.stringify(data)
fs.writeFile(filePath, text, err => {
if (err) {
console.error(err)
}
})
}
function readData() {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
const data = JSON.parse(data)
// handle the json data here
console.log(data)
});
}
In the example code above, the data is persisted in the current user's userData
directory. On windows, the file path looks like C:\Users\<user-name>\AppData\Roaming\<app-name>\data.json
.