I have an Electron app, based off of the electron-react-boilerplate template project. In my app, I have a class in a myClass.js
file in
app/utils/myClass.js
class MyClass {
// A bunch of stuff...
}
module.exports = MyClass;
I reference this class in a renderer process preload script.
app/utils/preload.js
const MyClass = require('./myClass.js');
Which I load in a renderer background worker process like so
const workerWindow = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: require('path').join(__dirname, 'utils', 'preload.js')
}
});
When I run the app in development/debug mode, the MyClass
module is found just fine and all code works. However, when I go to package the app (using electron-builder) and install on Windows, running the code causes the following error:
{"code":"MODULE_NOT_FOUND","requireStack":["C:\\Users\\Cooper\\AppData\\Local\\Programs\\my-app\\resources\\app\\utils\\myClass.js","C:\\Users\\Cooper\\AppData\\Local\\Programs\\my-app\\resources\\app\\utils\\preload.js"]}
For some reason, the app can't find my MyClass module, even though I can confirm the files are in the appropriate directory in the packaged .asar archive. I've tried many different ways of require
ing MyClass but to no avail.
Why can't my packaged app find my MyClass module? How can I resolve this? Worth noting my preoload.js script has no problem with any other dependency. I can do require('electron')
without issue, for example.