6

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 requireing 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.

Display name
  • 1,109
  • 1
  • 15
  • 31

1 Answers1

0

The issue ended up being that the app wasn't ablet find the MyClass module, but instead it couldn't find a dependency within MyClass. electron-builder uses two package.json files. package.json at the project root, which is used during development and did include all of my dependencies as expected, but also a second /app/package.json which is used for when the app is packaged and deployed. This was missing a lot of my dependencies.

https://www.electron.build/tutorials/two-package-structure

Manually adding the required dependencies to that package.json file resolved the issue.

Display name
  • 1,109
  • 1
  • 15
  • 31