Lesson 13 of 28 · javascript
Auto‑Updates with electron‑updater
Duration: 13 mins
Auto‑Updates with electron‑updater
electron‑updater (part of electron‑builder) abstracts away the platform‑specific update mechanisms (Squirrel on Windows, NSIS, macOS app‑store, etc.).
Install
npm install electron-updater
Basic usage in main.js
const { autoUpdater } = require('electron-updater');
autoUpdater.checkForUpdatesAndNotify();
autoUpdater.on('update-available', info => {
console.log('Update available', info.version);
});
autoUpdater.on('update-downloaded', info => {
const choice = dialog.showMessageBoxSync({
type: 'question',
buttons: ['Later', 'Install now'],
defaultId: 1,
message: `Version ${info.version} downloaded. Install now?`
});
if (choice === 1) autoUpdater.quitAndInstall();
});
Packaging for updates
- Use electron‑builder to create a GitHub release (or any server) that hosts the
*.exe,*.dmg,*.AppImagefiles. - Set the
publishfield inpackage.json:
"build": {
"publish": [{ "provider": "github", "owner": "myuser", "repo": "my‑app" }]
}
Now each new release is automatically detected by autoUpdater.