Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

javascript - 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 distwin-unpacked esourcesapp.asa (which is a file).

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.binelectron.cmd srcmainindex.js

from unpacked source distwin-unpackedapp.exe check-for-update

from installed folder C:UsersuserAppDataLocalProgramsappapp.exe

+-- dist
|   +-- win-unpacked
|      +-- resources
|         +-- extraResources
|            config.json
+-- node_modules
+-- src 
|   +-- extraResources
|      config.json
|      someFile.js
|   +-- main
|      index.js
|   +-- render
|      index.js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...