Electron - How to add external files?
Managed to solve it by using extraResources. Should be declared under build in your package.json
file.
For example:
Create a new folder named extraResources adjacent to pacakge.json
Add the following code to your
package.json
file:"build": {
"extraResources": ["./extraResources/**"]
}Then, you can access the files inside this folder by using
__dirname + '/../extraResources/'
from your main app.
Electron-Builder include external folder
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.
Adding external javascript file in javascript
Since electron
is node
based, you can use require
both in your main process JS file and in any JS file your project uses.
// main.js or something
let myModule = require("./myModule");
// myModule.js
....
module.exports = function() {}; // or whatever you want to share with main.js
Packaging Electron Apps with external files
look into process.resourcesPath
Related Topics
To Check If a String Is Alphanumeric in JavaScript
Get High-Order Block'S Id on Click
Javascript Ajax Not Working on ASP.NET C# and Returns a 404
Onchange Event Updates State With 1 Character Delay
How to Convert a Htmlelement to a String
Onclick Event Function in JavaScript
How to Show Div When a Radio Button Is Clicked in React
Updating Value of Multidimensional Array of Object
404 Error When Trying to Register Serviceworker
How to Send One Alert from My Node.Js to My JavaScript Client
Bootstrap Modal Only Showing Backdrop
Datatables/Tabletools: Format Data as Text When Exporting to Excel
How to Compare Two Objects and Get Key-Value Pairs of Their Differences
Call Method That Inside Another Component - React Js
Display a Loading Icon While Images Loads