2

I am writing an Electron program and want to use some javascript classes in the main process. I would prefer to have those classes in different files and include them inside of the script.

I usually did this by just adding both, the script and the classes inside the html file but since it is the main process there is not html file.

Question Is it possible to include an external javascript file in a different javascript file?

niklassc
  • 597
  • 1
  • 9
  • 24

2 Answers2

5

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
nadavvadan
  • 3,930
  • 1
  • 17
  • 29
1

jQuery has a method called getScript() which can be used to load external scripts, https://api.jquery.com/jquery.getscript/

$.getScript('https://example.com/js/another.js');
Jason Heithoff
  • 508
  • 3
  • 10