Search code examples
webpackelectronelectron-builderasar

Packaging Electron app into ASAR causes module not found error


I am having an issue packing my application into a single .asar file. I am using electron-builder to initiate packing of the .asarfile. If I do not package the app into an asar, it works as expected if I unpack the generated asar, it also works as expected.

However, running the electron using the asar produces this error:

Error: Cannot find module './MyModuleBase' at Module._resolveFilename (internal/modules/cjs/loader.js:584:15)

Previously, the asar was working without issue but we did some refactoring in the project splitting code out into a new module.

**Initial Configuration (ASAR Working) **

+--electron-application
  +--application

electron-application depends on the application module.

**New Configuration (ASAR Broken) **

+--electron-application
  +--application
    +--application-core

electron-application depends on the application module and application depends on application-core MyModuleBase is in the application-core module

One other point which may be relevant: The application is being packaged with web-pack using the webpack-stream module.

Update: I've solved the problem, however I would still like to know why the .asar version would fail and extracting the files would work.


Solution

  • After digging into this further, it looks like the root cause of the problem is further upstream. The main process was referencing a class in application, however main process is not being webpacked. The class has been moved to main, and now everything works as expected. It appears the Electron app was able to resolve the files in the unpackaged state, but not the packaged state.