Using Nullsoft Scriptable Install System (NSIS) to create an installer for your Electron app involves packaging your app's files into a Windows installer. Below is a step-by-step overview of the process, including key considerations and a sample script.
Step 1: Prepare Your Electron App
Before creating the installer, ensure your Electron app is properly packaged. Use tools like:
- electron-packager (for simple packaging)
- electron-builder (for advanced features like auto-updates, signing, etc.)
This will generate a folder containing:
- Your app's executable (
your-app.exe)
- Resources (HTML, CSS, JS, icons, etc.)
- Dependencies (Node modules, etc.)
Example structure:
your-electron-app/
├── app/
│ ├── your-app.exe
│ ├── assets/
│ └── ... (other files)
└── package.json
Step 2: Install NSIS
Download and install NSIS from https://nsis.sourceforge.io/Download. This includes the compiler (makensis.exe).
Step 3: Create an NSIS Script
Create a .nsi file (e.g., installer.nsi) with the following structure:
; Define the name of the installer
Name "YourElectronApp"
; Define the output file
OutFile "YourElectronAppInstaller.exe"
; Set the installation directory
InstallDir "$PROGRAMFILES\YourElectronApp"
; Include the standard header
!include "MUI.nsh"
; Define the installation page
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
; Define the uninstaller
!insertmacro MUI_UNPAGE_INSTFILES
; Define the installation section
Section "Install"
; Create the installation directory
CreateDirectory "$INSTDIR"
; Copy your app's files
SetOutPath "$INSTDIR"
File /r "path\to\your-electron-app\app\*.*"
; Create a shortcut on the desktop
CreateShortcut "$DESKTOP\YourElectronApp.lnk" "$INSTDIR\your-app.exe"
; Register the app (optional)
WriteRegStr HKLM "Software\YourElectronApp" "InstallDir" "$INSTDIR"
SectionEnd
; Define the uninstallation section
Section "Uninstall"
; Delete the shortcut
Delete "$DESKTOP\YourElectronApp.lnk"
; Remove the installation directory
RMDir /r "$INSTDIR"
; Remove the registry entry
DeleteRegKey HKLM "Software\YourElectronApp"
SectionEnd
Step 4: Compile the Installer
Run the NSIS compiler (makensis.exe) to generate the installer:
makensis.exe installer.nsi
This creates a .exe file in the same directory as your script.
Step 5: Customize the Installer
- Add icons: Use
Icon and UninstallIcon directives.
- Silent installation: Add
SilentInstall to the OutFile line.
- Check for dependencies: Use
IfFileExists to verify prerequisites (e.g., Node.js).
- Auto-updates: Integrate with Electron's built-in update system (requires additional logic).
Key Considerations
- File Paths: Ensure the script references the correct paths to your Electron app's build folder.
- Permissions: Use
RequestExecutionLevel admin if the app requires elevated privileges.
- Uninstaller: The
Uninstall section must cleanly remove all files and registry entries.
- Testing: Test the installer on different Windows versions (32-bit vs. 64-bit).
Example Script Notes
- Replace
path\to\your-electron-app\app\*.* with the actual path to your app's build folder.
- Adjust
your-app.exe to match your app's executable name.
- Use
!include "MUI.nsh" for modern UI (optional).
Advanced Features
- Custom UI: Use
MUI macros for a polished installer.
- Compression: Add
SetCompressor /SOLID for smaller installers.
- Digital Signatures: Use tools like
signtool.exe to sign the installer.
By following these steps, you can create a professional installer for your Electron app using NSIS. Let me know if you need help tailoring the script for specific requirements!