Files
velopack/docfx/docs/getting-started/electron.md
2024-02-27 22:55:06 +00:00

2.1 KiB

Applies to: Windows, MacOS, Linux

Getting Started: JS / Electron

This quick start is slightly tailored to Electron, so if you are using pure nodejs and bundling your app using pkg or similar you can ignore those steps.

  1. Add Velopack to your package.json:
npm install velopack
  1. Add the following code to your entry point (eg. index.js) as early as possible (before any electron startup code etc.):
const { VelopackApp } = require('velopack');

// Velopack builder needs to be the first thing to run in the main process.
// In some cases, it might quit/restart the process to perform tasks.
VelopackApp.build().run();

// ... your other app startup code here
  1. Add auto-updates somewhere to your app:
const { UpdateManager } = require('velopack');

async function updateApp()
{
    const um = new UpdateManager();
    um.setUrlOrPath("https://the.place/you-host/updates");

    const updateInfo = await um.checkForUpdatesAsync();
    if (!updateInfo) {
        return; // no update available
    }

    await um.downloadUpdatesAsync(updateInfo.targetFullRelease, p => {
        console.log(`progress: ${p}%`);
    });

    um.applyUpdatesAndRestart(updateInfo.targetFullRelease);
}
  1. If you are using electron/forge, you will need to add an asar unpack rule:
module.exports = {
  packagerConfig: {
    asar: {
      // velopack contains native binaries which must remain unpacked
      unpack: '**/node_modules/velopack/**',
    },
  },
}
  1. Compile your app to a binary (eg. .exe on Windows). Example using electron forge:
npx electron-forge package
  1. Install the vpk command line tool:
dotnet tool update -g vpk

Note: you must have the .NET Core SDK 6 installed to use and update the vpk

  1. Package your Velopack release / installers:
vpk pack -u MyAppUniqueId -v 1.0.0 -p /myBuildDir -e myexename.exe

You're Done! Your app now has auto-updates and an installer. You can upload your release to your website, or use the vpk upload command to publish it to the destination of your choice.