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?
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')
});
}
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With