2017-08-11 23:04:25 +00:00
# Updating Applications
2017-08-14 20:26:33 +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
Electron's [autoUpdater ](../api/auto-updater.md ) module.
2017-08-11 23:04:25 +00:00
2017-08-14 20:26:33 +00:00
## Deploying an update server
2017-08-11 23:04:25 +00:00
2017-08-14 20:26:33 +00:00
To get started, you first need to deploy a server that the
[autoUpdater ](../api/auto-updater.md ) module will download new updates from.
2017-08-11 23:04:25 +00:00
Depending on your needs, you can choose from one of these:
2017-08-21 22:19:59 +00:00
- [Hazel ](https://github.com/zeit/hazel ) – Simple update server for open-source
apps. Pulls from
[GitHub Releases ](https://help.github.com/articles/creating-releases/ )
and can be deployed for free on [Now ](https://zeit.co/now ).
2017-08-14 20:26:33 +00:00
- [Nuts ](https://github.com/GitbookIO/nuts ) – Also uses
[GitHub Releases ](https://help.github.com/articles/creating-releases/ ),
but caches app updates on disk and supports private repositories.
- [electron-release-server ](https://github.com/ArekSredzki/electron-release-server ) –
Provides a dashboard for handling releases
2017-11-20 16:04:14 +00:00
- [Nucleus ](https://github.com/atlassian/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.
2017-08-11 23:04:25 +00:00
2017-09-27 05:11:21 +00:00
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.
2017-08-22 14:51:57 +00:00
2017-08-14 20:26:33 +00:00
## Implementing updates in your app
2017-08-11 23:04:25 +00:00
2017-08-14 20:26:33 +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
[Hazel ](https://github.com/zeit/hazel ).
2017-08-11 23:23:54 +00:00
2017-08-14 20:26:33 +00:00
**Important:** Please ensure that the code below will only be executed in
2017-08-15 19:53:46 +00:00
your packaged app, and not in development. You can use
2017-08-14 20:26:33 +00:00
[electron-is-dev ](https://github.com/sindresorhus/electron-is-dev ) to check for
the environment.
2017-08-11 23:04:25 +00:00
```js
2017-08-21 22:19:59 +00:00
const {app, autoUpdater, dialog} = require('electron')
2017-08-11 23:04:25 +00:00
```
2017-08-14 20:26:33 +00:00
Next, construct the URL of the update server and tell
[autoUpdater ](../api/auto-updater.md ) about it:
2017-08-11 23:04:25 +00:00
```js
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
```js
2017-08-12 10:48:49 +00:00
setInterval(() => {
autoUpdater.checkForUpdates()
}, 60000)
2017-08-11 23:04:25 +00:00
```
2017-08-15 19:53:46 +00:00
Once your application is [packaged ](../tutorial/application-distribution.md ),
it will receive an update for each new
2017-08-14 20:26:33 +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
2017-08-15 19:53:46 +00:00
## Applying updates
2017-08-11 23:04:25 +00:00
2017-08-14 20:26:33 +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
2017-08-15 19:53:46 +00:00
can be achieved using the autoUpdater API
[events ](../api/auto-updater.md#events ):
2017-08-11 23:04:25 +00:00
2017-08-15 09:38:54 +00:00
```js
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
2017-08-15 20:04:52 +00:00
dialog.showMessageBox(dialogOpts, (response) => {
2017-08-15 19:53:46 +00:00
if (response === 0) autoUpdater.quitAndInstall()
})
2017-08-15 09:38:54 +00:00
})
```
Also make sure that errors are
[being handled ](../api/auto-updater.md#event-error ). Here's an example
for logging them to `stderr` :
```js
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