Auto-updates
An auto-update checks for a new version at startup, downloads it in the background and installs it on the next restart. Without it, users stay on an old version with bugs and vulnerabilities for years. In Electron it needs two parts: a place where new versions live, and code in the app that checks for them.
How it works
- You increase
versioninpackage.jsonand build the app. - The build is published to an update server, for example GitHub Releases.
- The installed app asks the server whether there is a newer version.
- If there is, it downloads it and offers to restart.
Electron's built-in autoUpdater module works on Windows (with the Squirrel installer) and on macOS. On Linux updates are usually delivered by the system's package manager.
Publishing to GitHub
Forge publishes builds through publishers. For GitHub:
npm install --save-dev @electron-forge/publisher-github// forge.config.js
module.exports = {
publishers: [
{
name: '@electron-forge/publisher-github',
config: {
repository: { owner: 'anna', name: 'notes' },
prerelease: false,
},
},
],
};npm run publish builds the installers and uploads them to a new release of the anna/notes repository. Access to GitHub needs a token in the GITHUB_TOKEN environment variable.
Checking for updates
Open-source apps on GitHub can use the free update.electronjs.org service and its module:
npm install update-electron-app// main.js
const { updateElectronApp } = require('update-electron-app');
updateElectronApp();The module reads the repository address from the repository field of package.json, checks for updates at startup and then periodically. When a new version is downloaded, it shows a dialog offering to restart the app.
The built-in autoUpdater
With your own update server, the autoUpdater module is used directly:
// main.js
const { autoUpdater } = require('electron');
autoUpdater.setFeedURL({ url: 'https://updates.example.com/notes' });
autoUpdater.on('update-downloaded', () => {
autoUpdater.quitAndInstall();
});
autoUpdater.checkForUpdates();update-downloaded fires when the new version is downloaded, and quitAndInstall restarts the app already updated. Usually the user is asked first with dialog.showMessageBox.
Other options
electron-builder — an alternative to Forge for building — has its own update module, electron-updater. It supports more formats, including AppImage for Linux. Do not mix it with Forge: pick one tool.
Common mistake
Updates are not checked when running with npm start: update modules work only in a built and installed app, and on macOS only in a signed one. Test auto-updates on real builds: install version 1.0.0, publish 1.0.1 and start the installed app.