简体   繁体   English

Electron 部署的应用程序未运行,找不到模块“electron-reload”

[英]Electron deployed app not running, can't find module 'electron-reload'

I'm learning Electron, so I'm using it for the first time ever by following this tutorial by DesignCourse .我正在学习 Electron,所以我是第一次按照DesignCourse 的教程使用它。 I constantly checked to make sure my code worked properly before proceeding.在继续之前,我不断检查以确保我的代码正常工作。 I can confirm that whenever I run it with npm start from the command prompt, it runs properly.我可以确认,每当我从命令提示符使用 npm start 运行它时,它都能正常运行。

However, whenever I attempted to deploy it into an application that can be sent to others, I got a few errors.然而,每当我试图将它部署到一个可以发送给其他人的应用程序中时,我都会遇到一些错误。 The biggest problem is that while the application was packaged, attempting to run the application gets me an error about a missing module 'electron-reload'.最大的问题是,当应用程序被打包时,尝试运行应用程序会给我一个关于缺少模块“electron-reload”的错误。 I have no idea how this module could have gone missing.我不知道这个模块怎么会丢失。 I've linked an image of the error here .我在此处链接了错误图片 How can I fix this to get my application to run?我该如何解决这个问题才能让我的应用程序运行?

There's another related issue, too.还有另一个相关的问题。 When I first packaged the app, I had no errors at the time.当我第一次打包应用程序时,我当时没有错误。 But if I try to package the app again, I get this error about asar .但是,如果我再次尝试打包该应用程序,则会收到有关 asar 的错误消息 I don't believe I changed anything else in my code between the first packaging and any subsequent attempts, except for a version number in my package.json file.我不相信我在第一次打包和任何后续尝试之间更改了我的代码中的任何其他内容,除了我的 package.json 文件中的版本号。 Why is this error showing up now and not before?为什么这个错误现在出现而不是之前出现?

EDIT: Woops, I forgot one little detail that might help, While I can run the app from the command prompt just fine.编辑:糟糕,我忘记了一个可能有帮助的小细节,虽然我可以从命令提示符运行该应用程序。 every time I do so I get an odd message.每次我这样做时,我都会收到一条奇怪的消息。 It says "Electron could not be found. No hard resets for you,", but the app still runs .它说“找不到 Electron。没有为您进行硬重置”,但该应用程序仍在运行 Why is that?这是为什么?

About the electron-reload issue, you probably only want to use electron-reload when in the development environment.关于electron-reload问题,你可能只想在开发环境中使用electron-reload The way I do this is described below (the approach is one suggested by other people – I think I just changed a few things).下面描述了我这样做的方法(该方法是其他人建议的——我想我只是改变了一些东西)。

I don't know about the asar issue.我不知道asar问题。 The start issue may have to do with how your project directory is set up and/or the start script you are using. start问题可能与项目目录的设置方式和/或您使用的start脚本有关。


In "package.json" – set an environment variable "APP_DEV" when calling "npm start":在“package.json”中——调用“npm start”时设置一个环境变量“APP_DEV”:

  "scripts": {
    "start": "APP_DEV=true electron .",
    // other scripts deleted for clarity (package etc.)
  }

(more details on Node "process" and "process.env" ). (有关节点“process”“process.env”的更多详细信息)。


In "main.js" check for the environment variable "APP_DEV".在“main.js”中检查环境变量“APP_DEV”。 If it exists (and is "true") then init electron-reload with the app directory and the path to the electron binary.如果它存在(并且为“真”),则使用 app 目录和electron二进制文件的路径初始化electron-reload The gist is that we only want to use electron-reload when in the development environment.要点是我们只想在开发环境中使用electron-reload

var isDev = process.env.APP_DEV ? (process.env.APP_DEV.trim() == "true") : false;

if (isDev) {
    require('electron-reload')(__dirname, {
        electron: path.join(__dirname, 'node_modules', '.bin', 'electron')
    });
}

As NG said, the issue was that I was trying to use electron-reload outside of a development environment.正如 NG 所说,问题是我试图在开发环境之外使用 electron-reload。 By simply removing the requirement of electron-reload, the program was able to successfully deploy and run as an application, without any of the three errors I shared in my question (Well, I still get a warning about asar not taking arguments, but the program gets successfully deployed this time).通过简单地删除 electron-reload 的要求,该程序能够成功部署并作为应用程序运行,没有我在问题中分享的三个错误中的任何一个(好吧,我仍然收到关于 asar 不接受参数的警告,但是这次程序成功部署)。 Big thanks to NG!非常感谢 NG!

You don´t nedd any extra flag, just go with if (.app.isPackaged) {require...} and you are done.您不需要任何额外的标志,只需使用if (.app.isPackaged) {require...}就可以了。

i've had this problem before.我以前遇到过这个问题。 it's solve by removing follwing line in main electron js.它通过删除主电子 js 中的以下行来解决。

 require('electron-reload')(_dirname)

As the others said, in your code remove any references to packages in "devDependencies" inside your package.json file.正如其他人所说,在您的代码中删除 package.json 文件中“devDependencies”中对包的任何引用。 They won't be bundled in your production builds and will break the app.它们不会捆绑在您的生产版本中,并且会破坏应用程序。

eg if using "devtron" then comment it out例如,如果使用“devtron”然后将其注释掉

// require("devtron").install(); -- doesn't exist!

I've just tried this code, it works for me.我刚刚试过这段代码,它对我有用。

if (process.env.NODE_ENV === 'development') { require('electron-reload')(__dirname) };

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

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