Search code examples
javascriptnode.jselectronelectron-builder

Electron - How to add external files?


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.

I tried writing to console the default location using console.log(__dirname) and it outputs \dist\win-unpacked\resources\app.asa (which is a file).

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


Solution

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

    For example:

    1. Create a new folder named extraResources adjacent to pacakge.json

    2. Add the following code to your package.json file:

      "build": {
          "extraResources": ["./extraResources/**"]
      }
      
    3. Then, you can access the files inside this folder by using __dirname + '/../extraResources/' from your main app.