9

Well, I'm working on a certain app that would improve work in the company. For this I would need to create, save and read a file without a dialog box.

I created this code with the help of documentation and the Internet:

const electron = require('electron');
let fs = require('fs'), app = electron.remote;
let localData, fileName = "appdata.json";

function loadAppData() {
    fs.readFile(fileName, (err, data) => {
        if (err) {
            console.log("There was a problem reading the data!");
            // console.log(err);
        } else {
            console.log("Data loaded!");

            localData = JSON.parse(data);
            console.log(localData);
        }
    });
}

function saveAppData(content) {
    content = JSON.stringify(content);

    fs.writeFile(fileName, content, (err) => {
        if (err) {
            console.log("There was a problem saving data!");
            // console.log(err);
        } else {
            console.log("Data saved correctly!");
            loadAppData();
        }
    });
}

function initappData() {
    if (fs.existsSync(fileName)) {
        console.log("File detected, loading");
        loadAppData();

    } else {
        let defData = {
            "patients": [],
            "actions": [],
            "visits": []
        };
        console.log("No file! I create! Saving! Loading!");
        saveAppData(defData);
    }
}
initappData();

And I have a problem because if the script works on the local version, after the application build on MacOS (using electron-builder) the error appears in the console: "There was a problem writing data!". After displaying the error content appears: Error: EROFS: read-only file system, open 'appdata.json'.

I checked permissions, I checked in other locations - still the same :( I was looking for a solution on the net but unfortunately nothing solved the problem.

Has anyone encountered such a problem?

1 Answer 1

10

After the build. The resource will be packaged inside asar file But this is just read-only. You can't modify the file inside of asar.

If I were you. I'm going to store the appData.json at Application Support. And I think this is the popular configure for application.

You can get the Application Data path by using this.

function getAppDataPath() {
  switch (process.platform) {
    case "darwin": {
      return path.join(process.env.["HOME"], "Library", "Application Support", "Your app name");
    }
    case "win32": {
      return path.join(process.env.APPDATA, "Your app name");
    }
    case "linux": {
      return path.join(process.env.["HOME"], ".Your app name");
    }
    default: {
      console.log("Unsupported platform!");
      process.exit(1);
    }
  }
}


function saveAppData(content) {
    const appDatatDirPath = getAppDataPath();
    
    // Create appDataDir if not exist
    if (!fs.existsSync(appDatatDirPath)) {
        fs.mkdirSync(appDatatDirPath);
    }

    const appDataFilePath = path.join(appDatatDirPath, 'appData.json');
    content = JSON.stringify(content);

    fs.writeFile(appDataFilePath, content, (err) => {
        if (err) {
            console.log("There was a problem saving data!");
            // console.log(err);
        } else {
            console.log("Data saved correctly!");
            loadAppData();
        }
    });
}
Sign up to request clarification or add additional context in comments.

9 Comments

Hi! Thanks for response. I want to ask, where I should use this code? In main running script and use it with ipcRenderer in index file or in index.html link script like normal HTML script?
You can use this anywhere. To use this code at your renderer then you should sure if the nodeIntegration is enabled when you are creating your browserWindow. But I'd recommend you to do this operation at your main. Of course, you can use ipc API if needs.
Thanks for advice :) So I put modified my and your code into index.html: gist.github.com/Mativve/6f33cd280e3387891e0d9cc3576b8488 In index.js (main file of electron) I have: gist.github.com/Mativve/2a5834a0eba6c43cfbf29afee24e5e0d Unfortunately after build my app in console an error appears: Uncaught ReferenceError: path is not defined at getAppDataPath (index.html:19) at saveAppData (index.html:50) [...] I tried find solution of 'path' variable or global variable but I found nothing interesting and solving my problem :/
You are not inclucing the const path = require('path')
@OrangeSubmarine121 Sorry for my long response. Here is the code with package.json, index.js and index.html -> gist.github.com/Mativve/b77098ce42e6947ea35ae6e1d6a28186 I hope that helps you ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.