2017-08-11 23:04:25 +00:00
# Updating Applications
2018-01-12 15:24:48 +00:00
There are several ways to update an Electron application. The easiest and
officially supported one is taking advantage of the built-in
[Squirrel ](https://github.com/Squirrel ) framework and
2017-08-14 20:26:33 +00:00
Electron's [autoUpdater ](../api/auto-updater.md ) module.
2017-08-11 23:04:25 +00:00
2018-04-27 17:49:42 +00:00
## Using `update.electronjs.org`
GitHub's Electron team maintains [update.electronjs.org], a free and open-source
2019-03-20 20:12:47 +00:00
webservice that Electron apps can use to self-update. The service is designed
2018-04-27 17:49:42 +00:00
for Electron apps that meet the following criteria:
- App runs on macOS or Windows
- App has a public GitHub repository
- Builds are published to GitHub Releases
- Builds are code-signed
The easiest way to use this service is by installing [update-electron-app],
a Node.js module preconfigured for use with update.electronjs.org.
Install the module:
```sh
npm install update-electron-app
```
Invoke the updater from your app's main process file:
```js
require('update-electron-app')()
```
2019-03-20 20:12:47 +00:00
By default, this module will check for updates at app startup, then every ten
minutes. When an update is found, it will automatically be downloaded in the background. When the download completes, a dialog is displayed allowing the user
2018-04-27 17:49:42 +00:00
to restart the app.
2019-03-20 20:12:47 +00:00
If you need to customize your configuration, you can
2018-04-27 17:49:42 +00:00
[pass options to `update-electron-app` ][update-electron-app]
2019-03-20 20:12:47 +00:00
or
2018-04-27 17:49:42 +00:00
[use the update service directly][update.electronjs.org].
## Using `electron-builder`
If your app is packaged with [`electron-builder`][electron-builder-lib] you can use the
[electron-updater] module, which does not require a server and allows for updates
from S3, GitHub or any other static file host. This sidesteps Electron's built-in
update mechanism, meaning that the rest of this documentation will not apply to
`electron-builder` 's updater.
2018-02-19 23:24:15 +00:00
## Deploying an Update Server
2017-08-11 23:04:25 +00:00
2018-04-27 17:49:42 +00:00
If you're developing a private Electron application, or if you're not
2019-03-20 20:12:47 +00:00
publishing releases to GitHub Releases, it may be necessary to run your own
2018-04-27 17:49:42 +00:00
update server.
2017-08-11 23:04:25 +00:00
Depending on your needs, you can choose from one of these:
2018-02-19 23:24:15 +00:00
- [Hazel][hazel] – Update server for private or open-source apps which can be
deployed for free on [Now][now]. It pulls from [GitHub Releases][gh-releases]
and leverages the power of GitHub's CDN.
- [Nuts][nuts] – Also uses [GitHub Releases][gh-releases], but caches app
updates on disk and supports private repositories.
- [electron-release-server][electron-release-server] – Provides a dashboard for
handling releases and does not require releases to originate on GitHub.
- [Nucleus][nucleus] – A complete update server for Electron apps maintained by
Atlassian. Supports multiple applications and channels; uses a static file store
to minify server cost.
## Implementing Updates in Your App
2017-08-11 23:04:25 +00:00
2018-01-12 15:24:48 +00:00
Once you've deployed your update server, continue with importing the required
modules in your code. The following code might vary for different server
software, but it works like described when using
2017-08-14 20:26:33 +00:00
[Hazel ](https://github.com/zeit/hazel ).
2017-08-11 23:23:54 +00:00
2018-01-12 15:24:48 +00:00
**Important:** Please ensure that the code below will only be executed in
your packaged app, and not in development. You can use
[electron-is-dev ](https://github.com/sindresorhus/electron-is-dev ) to check for
2017-08-14 20:26:33 +00:00
the environment.
2017-08-11 23:04:25 +00:00
2018-02-19 23:24:15 +00:00
```javascript
const { app, autoUpdater, dialog } = require('electron')
2017-08-11 23:04:25 +00:00
```
2018-01-12 15:24:48 +00:00
Next, construct the URL of the update server and tell
2017-08-14 20:26:33 +00:00
[autoUpdater ](../api/auto-updater.md ) about it:
2017-08-11 23:04:25 +00:00
2018-02-19 23:24:15 +00:00
```javascript
2017-08-14 20:26:33 +00:00
const server = 'https://your-deployment-url.com'
2017-08-11 23:04:25 +00:00
const feed = `${server}/update/${process.platform}/${app.getVersion()}`
2017-08-12 10:48:49 +00:00
autoUpdater.setFeedURL(feed)
2017-08-11 23:04:25 +00:00
```
2017-08-21 22:19:59 +00:00
As the final step, check for updates. The example below will check every minute:
2017-08-11 23:04:25 +00:00
2018-02-19 23:24:15 +00:00
```javascript
2017-08-12 10:48:49 +00:00
setInterval(() => {
autoUpdater.checkForUpdates()
}, 60000)
2017-08-11 23:04:25 +00:00
```
2018-01-12 15:24:48 +00:00
Once your application is [packaged ](../tutorial/application-distribution.md ),
2018-02-19 23:24:15 +00:00
it will receive an update for each new
2018-01-12 15:24:48 +00:00
[GitHub Release ](https://help.github.com/articles/creating-releases/ ) that you
2017-08-15 19:53:46 +00:00
publish.
2017-08-11 23:04:25 +00:00
2018-02-19 23:24:15 +00:00
## Applying Updates
2017-08-11 23:04:25 +00:00
2018-01-12 15:24:48 +00:00
Now that you've configured the basic update mechanism for your application, you
2017-08-15 09:38:54 +00:00
need to ensure that the user will get notified when there's an update. This
2018-01-12 15:24:48 +00:00
can be achieved using the autoUpdater API
2017-08-15 19:53:46 +00:00
[events ](../api/auto-updater.md#events ):
2017-08-11 23:04:25 +00:00
2018-02-19 23:24:15 +00:00
```javascript
2017-08-15 09:38:54 +00:00
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
2017-08-15 19:53:46 +00:00
const dialogOpts = {
type: 'info',
buttons: ['Restart', 'Later'],
title: 'Application Update',
2017-08-15 20:55:55 +00:00
message: process.platform === 'win32' ? releaseNotes : releaseName,
detail: 'A new version has been downloaded. Restart the application to apply the updates.'
2017-08-15 19:53:46 +00:00
}
2017-08-15 20:09:06 +00:00
2019-11-18 08:49:50 +00:00
dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) autoUpdater.quitAndInstall()
2017-08-15 19:53:46 +00:00
})
2017-08-15 09:38:54 +00:00
})
```
2018-01-12 15:24:48 +00:00
Also make sure that errors are
2017-08-15 09:38:54 +00:00
[being handled ](../api/auto-updater.md#event-error ). Here's an example
for logging them to `stderr` :
2018-02-19 23:24:15 +00:00
```javascript
2017-08-15 19:38:31 +00:00
autoUpdater.on('error', message => {
console.error('There was a problem updating the application')
console.error(message)
})
2017-08-15 20:55:55 +00:00
```
2017-08-22 14:51:57 +00:00
[electron-builder-lib]: https://github.com/electron-userland/electron-builder
2017-09-12 19:37:29 +00:00
[electron-updater]: https://www.electron.build/auto-update
2018-02-19 23:24:15 +00:00
[now]: https://zeit.co/now
[hazel]: https://github.com/zeit/hazel
[nuts]: https://github.com/GitbookIO/nuts
[gh-releases]: https://help.github.com/articles/creating-releases/
[electron-release-server]: https://github.com/ArekSredzki/electron-release-server
[nucleus]: https://github.com/atlassian/nucleus
2018-04-27 17:49:42 +00:00
[update.electronjs.org]: https://github.com/electron/update.electronjs.org
2019-03-21 19:15:55 +00:00
[update-electron-app]: https://github.com/electron/update-electron-app