简体   繁体   English

打包的电子应用程序找不到本地模块

[英]Packaged Electron app can't find local module

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在我的应用程序中,我在myClass.js文件中有一个类

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.当我在开发/调试模式下运行应用程序时,发现MyClass模块很好,所有代码都可以正常工作。 However, when I go to package the app (using electron-builder) and install on Windows, running the code causes the following error:但是,当我打包应用程序(使用电子生成器)并安装在 Windows 上时,运行代码会导致以下错误:

{"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.出于某种原因,该应用程序找不到我的 MyClass 模块,即使我可以确认这些文件位于打包的 .asar 存档中的相应目录中。 I've tried many different ways of require ing MyClass but to no avail.我尝试了许多不同的方式来require MyClass 但无济于事。

Why can't my packaged app find my MyClass module?为什么我打包的应用程序找不到我的 MyClass 模块? How can I resolve this?我该如何解决这个问题? Worth noting my preoload.js script has no problem with any other dependency.值得注意的是,我的 preoload.js 脚本与任何其他依赖项都没有问题。 I can do require('electron') without issue, for example.例如,我可以毫无问题地执行require('electron')

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 .问题最终是应用程序无法找到MyClass模块,而是在MyClass找不到依赖项。 electron-builder uses two package.json files. electron-builder 使用两个 package.json 文件。 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. package.json 位于项目根目录,在开发过程中使用,并按预期包含我的所有依赖项,但还有第二个 /app/package.json 用于打包和部署应用程序。 This was missing a lot of my dependencies.这缺少我的很多依赖项。

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

Manually adding the required dependencies to that package.json file resolved the issue.将所需的依赖项手动添加到该 package.json 文件解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM