Electron - How to add external files?
Asked Answered
L

5

37

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?

Liquesce answered 3/9, 2017 at 10:23 Comment(0)
L
51

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.

Liquesce answered 4/9, 2017 at 8:51 Comment(6)
use process.resourcesPath instead of __dirname for a more reliable cross-platform solution.Mathur
This worked for me: const path = require('path'); path.join(process.resourcePath, 'extraResources', 'filename'); Hydantoin
Inside this extraResources directory I have a file timer.sh when I trying your method, then I am getting this error Uncaught Error: ENOENT, extraResources/timer.sh not found in /opt/ProjectName/resources/app.asarVaporescence
Thank you this really helped me a ton!Wet
@Hydantoin there is a typo in your code, it should be process.resourcesPath.Weisshorn
How does this work during development though? These files don't seem to be copied when electron . is run in the cwd during developmentCocks
A
40

Add the following code to package.json:

 "build": {
    "extraResources": [
      {
        "from": "./src/extraResources/",
        "to": "extraResources",
        "filter": [
          "**/*"
        ]
      }
    ]
  }

Then, you can access the files using

const configFile = path.join(path.dirname(__dirname), 'extraResources','config.json');

I use the following folders structure which allows me to run the app any way.

from project folder: node_modules\.bin\electron.cmd src\main\index.js

from unpacked source dist\win-unpacked\app.exe check-for-update

from installed folder C:\Users\user\AppData\Local\Programs\app\app.exe

+-- dist
|   +-- win-unpacked
|      +-- resources
|         +-- extraResources
|            config.json
+-- node_modules
+-- src 
|   +-- extraResources
|      config.json
|      someFile.js
|   +-- main
|      index.js
|   +-- render
|      index.js
Avra answered 26/10, 2018 at 14:52 Comment(1)
Hi @swaroop-bekal, I tried it under windows but doesn't works. The folder extraResources are correcty created but if I launch the application I have this messages: Uncaught Error: Cannot find module '\extraResources\config.json'Anyhow
F
9

there I found a new solution, using electron-packager on Windows do not add the files into the resources folder at the end of the process.

So I added this command into the package.json

"build-win": "electron-packager . --platform=win32 --asar --prune --arch=ia32 --extra-resource=./extraResources/documents/QuickStartGuideWN-H1.pdf --extra-resource=./extraResources/MAC_drivers/MacOS10.14/ --icon=assets/alfa_a.ico --out ./dist --overwrite",

And now the files are insied the resource fodlder just add

--extra-resource=./extraResources/file
Flexible answered 2/9, 2019 at 15:25 Comment(0)
C
0
"build": {
    "extraResources": [{
        "from": "./public/assets/file.html",
        "to": "file.html",
        "filter": [
          "**/*"
        ]
      }]}

"./public/assets/file.html" This file existe in your develepmont directory, and it will be copied to resources folder "C:\Users\yourname\AppData\Local\Programs\softwarename\resources\file.html"

Colza answered 26/10, 2023 at 6:15 Comment(0)
O
0

With electron-forge, in the forge.config.js file, add the extraResources option:

module.exports = {
  packagerConfig: {
    asar: true,
    extraResource: ['path/to/yourResource.jar'],
  },
  ...

Osbourne answered 7/6, 2024 at 13:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.