I'd like to require a module on a folder, as a plugin. So I want the user to be able to add JavaScript files into an already compiled electron/webpack application and having my application load and execute it. So it would be like a plugin system. I have tried requiring every file inside the plugins/ folder but it turns out that it just gets bundled into bundle.js when compiled, and I want to be able to change it after compiled, like a plugin. How can I accomplish this?
Asked
Active
Viewed 2,049 times
1 Answers
2
I think what you're looking for is global.require
as stated in this similar question.
Note that as it's Node's require
, it will cache required module, so modifying a plugin's code will not have effect until you restart your electron application so that it does call global.require
again. If that is an issue, you can force-reload a specific module with this (unrecommended) snippet:
delete global.require.cache[global.require.resolve(moduleName)]

naholyr
- 530
- 3
- 8
-
It worked, but only when I require absolute paths.. I want to be able to access my relative path or just an easy way to access data instead of typing the whole static address – John Doe Mar 17 '18 at 23:58
-
Couldn't [getAppPath()](https://stackoverflow.com/a/37215237/1423359) solve this issue? So you can build absolute paths dynamically from current application's path (btw you will want to read the link's next comment about how this might be a bad idea to mess with this folder, maybe letting user put his plugins in his home directory could be better). – naholyr Mar 18 '18 at 00:09
-
I am looking at the same problem atm. But using `global.require` throws an error saying `global.require is not a function` . Any ideas what could go wrong ? I am using `vue-cli-plugin-electron-build` to create an electron app with vue. – turbopasi Jun 16 '20 at 10:03