How to Implement Auto-Updates in Electron Apps Using autoUpdater
Electron's autoUpdater module provides a unified, cross-platform API that enables applications to automatically check for updates, download them silently, and install them upon restart by delegating to platform-specific backends like Squirrel.Mac and Squirrel.Windows.
The electron/electron repository implements auto-updates in Electron apps through the autoUpdater module available exclusively in the main process. This module abstracts native update frameworks into a single JavaScript interface, allowing developers to ship seamless update experiences across macOS and Windows without managing platform-specific binaries manually.
How Electron autoUpdater Works Under the Hood
When the main process requires autoUpdater, the selector in lib/browser/api/auto-updater.ts checks process.platform and, for Windows, whether the app runs as an MSIX package (process.windowsStore). It then loads the appropriate implementation while exposing a consistent Electron.AutoUpdater interface.
Platform-Specific Backend Implementations
- macOS: Wraps Squirrel.Mac via the
electron_browser_auto_updaternative binding, implemented inlib/browser/api/auto-updater/auto-updater-native.ts. - Windows (Squirrel): Uses the internal
squirrel-update-winhelper to communicate with the Squirrel Windows updater, defined inlib/browser/api/auto-updater/auto-updater-win.ts. - Windows (MSIX): Delegates to the MSIX-specific updater in
lib/browser/api/auto-updater/auto-updater-msix.ts, supporting direct.msixURLs and JSON manifests. - Linux/Other: The generic entry point handles platform detection and module loading, ensuring the API surface remains identical across operating systems.
All implementations inherit from EventEmitter, emitting lifecycle events documented in docs/api/auto-updater.md.
Core autoUpdater API and Event Lifecycle
The module exposes three primary methods that orchestrate the update process across all platforms.
Configuring the Update Feed
Call setFeedURL(options) to specify the server endpoint hosting your updates. This method stores the URL and optional headers for authenticated requests.
const { autoUpdater } = require('electron');
autoUpdater.setFeedURL({
url: 'https://example.com/updates/latest',
headers: { Authorization: 'Bearer token' }, // Optional: for authenticated feeds
serverType: 'json', // Optional: macOS only
allowAnyVersion: false // Optional: MSIX only
});
Checking and Downloading Updates
Invoke checkForUpdates() to initiate the network request. This emits the checking-for-update event, followed by either update-available or update-not-available.
On Windows, lib/browser/api/auto-updater/auto-updater-win.ts delegates the download to squirrelUpdate.update(url). On macOS, the native binding in auto-updater-native.ts streams the package automatically once an update is detected.
autoUpdater.on('checking-for-update', () => {
console.log('Checking for update...');
});
autoUpdater.on('update-available', (info) => {
console.log('Update available, downloading...');
});
autoUpdater.on('update-not-available', () => {
console.log('No update available.');
});
autoUpdater.on('error', (err) => {
console.error('Update error:', err.message);
});
Installing Updates
Once the download completes, the module emits update-downloaded. Calling quitAndInstall() then terminates the application and triggers the platform-specific installer.
On Windows, quitAndInstall() in auto-updater-win.ts invokes squirrelUpdate.processStart() before quitting to ensure proper shortcut handling. On macOS, the native binding handles the installation atomically.
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
dialog.showMessageBox({
type: 'question',
buttons: ['Install', 'Later'],
title: 'Update Ready',
message: `Version ${releaseName} is ready to install.`,
detail: releaseNotes
}).then(({ response }) => {
if (response === 0) autoUpdater.quitAndInstall();
});
});
Platform-Specific Implementation Details
Each platform imposes unique constraints on the auto-update mechanism.
macOS Code Signing and ATS Requirements
Squirrel.Mac requires the application to be code-signed; unsigned apps will fail silently or refuse to update. Additionally, App Transport Security (ATS) blocks HTTP requests by default. You must add the NSAllowsArbitraryLoads key to your Info.plist if your update server uses HTTP rather than HTTPS.
Windows Squirrel First-Run Handling
Squirrel.Windows passes the --squirrel-firstrun flag during the initial launch after installation. At this moment, the updater holds a file lock that can interfere with update checks. Defer checkForUpdates() by several seconds or skip the check entirely during this phase.
if (process.platform === 'win32' && process.argv.includes('--squirrel-firstrun')) {
setTimeout(() => autoUpdater.checkForUpdates(), 10000);
} else {
autoUpdater.checkForUpdates();
}
Ensure your App User Model ID set via app.setAppUserModelId() matches the ID used during packaging, or the updater will fail to locate the application.
Windows MSIX Downgrade Support
The MSIX implementation in lib/browser/api/auto-updater/auto-updater-msix.ts supports the allowAnyVersion: true option in setFeedURL(), enabling downgrades to previous versions. The feed URL can point directly to an .msix file or a JSON manifest using the same schema as Squirrel.Mac.
Practical Implementation Examples
Basic Main Process Setup
// main.js
const { app, autoUpdater, dialog } = require('electron');
autoUpdater.setFeedURL({ url: 'https://your-server.com/updates' });
autoUpdater.on('update-downloaded', (event, notes, name) => {
const choice = dialog.showMessageBoxSync({
type: 'question',
buttons: ['Install Now', 'Later'],
defaultId: 0,
title: 'Application Update',
message: `Version ${name} available`,
detail: 'A new version has been downloaded. Restart to apply updates?'
});
if (choice === 0) autoUpdater.quitAndInstall();
});
app.whenReady().then(() => {
autoUpdater.checkForUpdates();
});
Handling Squirrel.Windows First-Run
function initializeUpdater() {
const isFirstRun = process.platform === 'win32' &&
process.argv.includes('--squirrel-firstrun');
if (isFirstRun) {
setTimeout(() => autoUpdater.checkForUpdates(), 15000);
} else {
autoUpdater.checkForUpdates();
}
}
MSIX-Specific Configuration
// For MSIX-packaged apps allowing version downgrades
autoUpdater.setFeedURL({
url: 'https://example.com/updates/msix-manifest.json',
allowAnyVersion: true
});
Summary
- Electron autoUpdater provides a cross-platform API in
lib/browser/api/auto-updater.tsthat selects the appropriate native backend based onprocess.platformandprocess.windowsStore. - Three core methods—
setFeedURL(),checkForUpdates(), andquitAndInstall()—manage the entire update lifecycle, emitting events via anEventEmitterinterface. - macOS requires code signing and proper ATS configuration in
Info.plistfor Squirrel.Mac to function. - Windows Squirrel apps must handle the
--squirrel-firstrunflag to avoid file-lock conflicts during initial launch. - Windows MSIX supports flexible versioning policies including downgrades via the
allowAnyVersionparameter.
Frequently Asked Questions
What server response format does autoUpdater require?
Squirrel.Mac and Squirrel.Windows expect either a JSON feed containing version metadata and release notes or specific server endpoints that return the appropriate update package. The MSIX implementation additionally accepts direct .msix file URLs. Refer to docs/tutorial/updates.md in the electron/electron repository for detailed server configuration examples.
Why does autoUpdater fail silently on macOS?
macOS requires the application to be code-signed with a valid Apple Developer ID certificate. Without signing, Squirrel.Mac refuses to install updates as a security measure. Additionally, ensure your update server uses HTTPS or explicitly disable App Transport Security via NSAllowsArbitraryLoads in your Info.plist.
How do I prevent update checks during Windows installation?
Check for the --squirrel-firstrun argument in process.argv before calling checkForUpdates(). During this initial launch, Squirrel.Windows holds a file lock that can cause update checks to fail. Implement a delay using setTimeout() or skip the check entirely until the second launch.
Can I downgrade application versions using Electron autoUpdater?
Standard Squirrel.Windows and Squirrel.Mac implementations only support upgrades. However, the MSIX backend in lib/browser/api/auto-updater/auto-updater-msix.ts supports downgrades when you set allowAnyVersion: true in the setFeedURL() options object. This permits the updater to install older versions when available on the server.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →