Search code examples
javascriptnpmelectronelectron-builder

Electron-Builder include external folder


I am building multiple electron apps and have one directory for common pictures and files. I would like to include them when building each app with electron-builder. The docs recommended -if I understood correctly- adding the path to the build > files key but it doesn't seem to work using this config file:

"build":{
    "files": [
        "**/*",
        "../common/img/*"
    ]
}

My directory structure is as follows:

|git_folder
|-- electronapp1
|---- package.json
|-- electronapp2
|---- package.json
|-- common
|---- img
|---- js
|---- css

I am trying to access the common directories with i.e. this HTML code <link rel="stylesheet" href="../common/css/master.css">. It works when starting it with electron . for debugging and developing, but when building with electron-builder, it doesn't seem to pack the common directories and throws "File not found" in the console.


Solution

  • In your configuration,

    "extraResources": [
        {
            "from": "../common",
            "to": "common"
        }
    ],
    "files": [
      "**/*"
    ],
    

    So if I were you I'll configure it like this

    const path = require("path");
    const appPath = __dirname;
    const appResourcePath = path.join(appPath, "..", "common")
    
    module.exports = {
      appPath,
      appResourcePath
    };
    

    Then you can use this appResourcePath anywhere at your renderer Such as

    <img src=path.join(appResourcePath, 'img', 'background.png')>
    

    Then this will be working in any environment.