作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个不需要重建项目就可以更改的文件。因此,我正在使用位于以下位置的 extraResources
:
.
├── extraResources
│ └── flags.json
├── src
├── package.json
└── ...
对于构建过程,我的 package.json
包含以下内容:
{
"build": {
"extraResources": [
"./extraResources/**"
]
}
}
这部分似乎有效,因为在构建输出中我在 dist/win-unpacked/resources/extraResources/flags.json
中获得了 extraResources
。
以上是按照以下步骤完成的:https://stackoverflow.com/a/46033483/2230045
我正在通过以下方式加载此文件:
const filepath = path.join(app.getAppPath(), 'extraResources', 'flags.json');
const data = fs.readFileSync(filepath, 'utf-8');
这在调试期间工作正常,但在构建之后,它失败了:
Uncaught Exception: Error: ENOENT, extraResources\flags.json not found in C:\dev\MyProgram\dist\win-unpacked\resources\app.asar
我不清楚为什么这对其他人似乎有效却不起作用。
最佳答案
这是因为 appPath
不同:
C:\dev\MyProgram\dist
packaged
目录中启动可执行文件时,例如 C:\dev\MyProgram\packages\win-unpacked\resources\app.asar
app.asar
C:\dev\MyProgram\packages\win-unpacked\resources\extraResources\flags.json
一个简单的解决方法是从路径中删除 asar
部分:
const appPath = app.getAppPath().replace('app.asar', '');
const filepath = path.join(appPath, 'extraResources', 'flags.json');
这在两种情况下都有效:即当它不存在于路径中时,路径不会改变
关于electron - 如何从 extraResources 中读取文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57360603/
我有一个不需要重建项目就可以更改的文件。因此,我正在使用位于以下位置的 extraResources: . ├── extraResources │ └── flags.json ├── src
我是一名优秀的程序员,十分优秀!