使用电子生成器打包后无法从 extraResources 读取

时间:2021-06-08 16:57:29

标签: javascript node.js electron

我正在使用一个应用程序,该应用程序从位于 extraResources 目录中的 json 文件读取/写入,由电子构建器创建

Package.json

"build": {
      "files": [
        "src/*",
        "src/json/*",
        "src/scss/*",
        "src/views/*"
      ],
      "extraResources": [
          {
            "from": "./src/json/",
            "to": "extraResources",
            "filter": [
              "**/*"
            ]
          }
      ],
      "win": {
        "target": "nsis",
        "icon": "build/icon.ico"
      }
    }

我试图用这个从 Tasks.json 读取 json 文件

const loadTasks = fs.readFileSync(path.join(__dirname, 'extraResources', "Tasks.json"))

但是在编译时,我在控制台中收到此错误消息: Uncaught Error: ENOENT: no such file or directory, open '/extraResources/Tasks.json'

我不知道我做错了什么,但我无法访问位于 app.asar 外部的 extraResources

谢谢!!!

1 个答案:

答案 0 :(得分:0)

electron-builder documentation 中,extraResources 位于应用程序的资源目录中

<块引用>

与项目目录相关的 glob 模式,指定时,将具有匹配名称的文件或目录直接复制到应用的资源目录中(MacOS 为 Contents/Resources,Linux 和 Windows 为资源)。

您可以使用 process.resourcesPath

获取资源目录
path.join(process.resourcesPath, YOUR_EXTRA_RESOURCES_FILES);