How to Package or Compile an Electron Js Application to a .exe?

I’ve been learning the Electron JS framework to build cross-platform desktop applications using web technologies like HTML, CSS, and JavaScript. I’m using the Electron Quick Start repository available on GitHub. This project runs smoothly using the following commands:

# Install dependencies
npm install
# Run the app
npm start

But, I’m struggling to compile this Electron application into a Windows executable (.exe) file.

First of all, you need the @electron/packager to Compile your electron application. You can Install the module with following command:

Install Command:

npm install --save-dev @electron/packager

Note: This module requires Node.js 16.13.0 or higher to run.

Packaging Command:

@electron/packager <sourcedir> <appname> --platform=<platform> --arch=<arch>

Note: --arch must be a string matching: ia32, x64, armv7l, arm64, mips64el

Complete Use:

Open your package.json file and find the ‘scripts’ section. Add a new entry called ‘build’ with the appropriate configuration for packaging, like this:

"scripts": {
    "start": "electron .",
    "build": "@electron/packager <sourcedir> <appname> --platform=win32 --arch=x64"
}

Now, whenever you want to build your app, simply run the command below.

npm run build