Packaging an Electron application as an .exe file for Windows is a common requirement. To achieve this, popular packaging tools such as electron-packager or electron-builder are commonly used. Below are the steps to use these tools to package an Electron project into an .exe file:
Using electron-packager
-
Install electron-packager If not already installed, you can install it via npm. Open your project directory in the terminal and run the following command:
bashnpm install electron-packager --save-dev -
Configure the packaging command In the project's
package.jsonfile, you can add a script to run electron-packager. For example:json"scripts": { "package-win": "electron-packager . MyApp --platform=win32 --arch=x64 --out=dist/" } -
Run the packaging command Execute the following command in the terminal to generate the .exe file:
bashnpm run package-winThis will generate a Windows application in the
dist/directory, including MyApp.exe.
Using electron-builder
-
Install electron-builder Install electron-builder via npm:
bashnpm install electron-builder --save-dev -
Configure electron-builder Set up electron-builder options in
package.json. For example:json"build": { "appId": "your.app.id", "win": { "target": "nsis", "icon": "build/icon.ico" } } -
Add the build script Include the following in the
scriptssection ofpackage.json:json"dist": "electron-builder --win" -
Execute the build command Run the following command to generate the installer:
bashnpm run distThis will produce an NSIS installer in the
distfolder under your project directory.
Summary
Both electron-packager and electron-builder can effectively package an Electron project into Windows executables. The choice depends on your specific needs; for more complex installation requirements (such as custom installation steps or auto-updates), electron-builder is a better choice. Each tool offers detailed documentation and community support, enabling you to select based on your project requirements.