Electron - 如何添加外部文件?

2022-01-10 00:00:00 node.js electron javascript

我有一个 Electron 应用程序.我尝试让应用程序打开一个 .exe 文件.我在名为 lib 的根文件夹中创建了一个目录,并将 .exe 文件放在那里.在开发中,我使用 __dirname + '/lib/file.exe 打开文件没有问题,但是当我打包应用程序时(使用 yarn dist),它确实不打开exe文件,dist文件夹下也没有lib文件夹了.

I have an Electron app. I try to make the app open an .exe file. I created a directory in the root folder named lib and placed the .exe file there. In development, I have no problem opening the file by using __dirname + '/lib/file.exe, but when I package the app (using yarn dist), it does not open the exe file and there is no lib folder anymore on the dist folder.

我尝试使用 console.log(__dirname) 写入控制台默认位置,它输出 distwin-unpackedesourcesapp.asa (即一个文件).

I tried writing to console the default location using console.log(__dirname) and it outputs distwin-unpackedesourcesapp.asa (which is a file).

如何添加应用打包时可以访问的外部文件?

How can I add an external file that can be accessed when the app is packaged?

推荐答案

设法通过使用 extraResources 来解决它.应该在 package.json 文件中的 build 下声明.

Managed to solve it by using extraResources. Should be declared under build in your package.json file.

例如:

  1. 在 pacakge.json 旁边创建一个名为 extraResources 的新文件夹
  2. 将以下代码添加到您的 package.json 文件中:

<代码>构建":{"extraResources": ["./extraResources/**"]}

相关文章