简体   繁体   English

Electron - 创建文件时出现问题,错误“EROFS:只读文件系统”

[英]Electron - problem creating file, error “EROFS: read-only file system”

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!".我有一个问题,因为如果脚本在本地版本上工作,在 MacOS 上构建应用程序(使用电子构建器)后,控制台中会出现错误:“写入数据时出现问题!”。 After displaying the error content appears: Error: EROFS: read-only file system, open 'appdata.json'.显示后出现错误内容: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?有没有人遇到过这样的问题?

After the build.建成后。 The resource will be packaged inside asar file But this is just read-only .资源将被打包在asar文件中,但这只是read-only You can't modify the file inside of asar .您不能修改asar的文件。

If I were you.如果我是你。 I'm going to store the appData.json at Application Support .appData.json存储在Application Support And I think this is the popular configure for application.我认为这是应用程序的流行配置。

You can get the Application Data path by using this.您可以使用它获取Application Data路径。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GAE 上的 NextJS - 错误:EROFS:只读文件系统 - NextJS on GAE - Error: EROFS: read-only file system Google 云函数部署:EROFS:只读文件系统 - Google Clould Functions deploy: EROFS: read-only file system 错误:EROFS:在 Lambda 中流式传输 xlsx 内容时只读文件系统 - Error: EROFS: read-only file system while streaming the xlsx content in Lambda DialogFlow Webhook:错误:EROFS:只读文件系统,打开“ .node-xmlhttprequest-sync-2” - DialogFlow Webhook : Error: EROFS: read-only file system, open '.node-xmlhttprequest-sync-2' 打开失败:EROFS(只读文件系统)android phonegap应用程序 - open failed: EROFS (Read-only file system) android phonegap app 运行Gulp任务时,Gulp抛出“解析文件时为只读”错误 - Gulp throwing “is read-only while parsing file” error while running Gulp task 使用filesaver.js创建一个只读文件 - Create a read-only file using filesaver.js 具有来自静态json文件的路由的主干应用程序(只读)? - Backbone application with routes from a static json file (read-only)? Electron 从对话框读取文件得到类型错误 - Electron read file from dialog box getting type error Node.js文件系统保存文件错误56 EROFS,每2秒保存一次 - Node.js filesystem save file error 56 EROFS while saving every 2 seconds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM