Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'. 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. 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. 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. Why is that?

like image 417
Chris H. Avatar asked Jan 28 '23 12:01

Chris H.


2 Answers

About the electron-reload issue, you probably only want to use electron-reload when in the development environment. 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. The start issue may have to do with how your project directory is set up and/or the start script you are using.


In "package.json" – set an environment variable "APP_DEV" when calling "npm start":

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

(more details on Node "process" and "process.env").


In "main.js" check for the environment variable "APP_DEV". If it exists (and is "true") then init electron-reload with the app directory and the path to the electron binary. The gist is that we only want to use electron-reload when in the development environment.

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')
    });
}
like image 94
spring Avatar answered Jan 31 '23 08:01

spring


As NG said, the issue was that I was trying to use electron-reload outside of a development environment. 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). Big thanks to NG!

like image 39
Chris H. Avatar answered Jan 31 '23 09:01

Chris H.