• 解决electron-builder制作的安装包在安装过程中出现“安装中止”的问题


    1. 删除残留的注册表信息。这种方法比较繁琐,但是可以解决问题。在安装新版本之前,你需要删除之前安装的版本所残留的注册表信息。
    2. 修改打包配置文件中的appid 但是如果appid是固定的 则不能使用这个方案
      这是我项目的打包配置
    electron-builder.json
    {
      "productName": "name",
      "appId": "appId",
      "directories": {
        "output": "release/v${version}"
      },
      "files": [
        "dist-electron",
        "dist"
      ],
      "publish": [
        {
          "provider": "generic",
          "url": ""
        }
      ],
      "asar": true,
      "nsis": {
        "oneClick": false,
        "perMachine": true,
        "shortcutName": "${productName}",
        "uninstallDisplayName": "${productName}",
        "runAfterFinish": true,
        "deleteAppDataOnUninstall": true,
        "allowToChangeInstallationDirectory": true,
        "include": "dist/installer.nsh",
        "guid": "b96a0cd0-6202-4088-9981-15fdd925595c"
      },
      "win": {
        "icon": "dist/favicon.ico",
        "artifactName": "${productName}.${ext}",
        "requestedExecutionLevel": "requireAdministrator"
      },
      "mac": {
        "icon": "dist/favicon.icns",
        "artifactName": "${productName}_${version}.${ext}",
        "target": [
          "dmg"
        ]
      },
      "linux": {
        "target": [
          "AppImage",
          "deb"
        ],
        "icon": "dist/favicon.ico",
        "artifactName": "${productName}.${ext}",
        "desktop": {
          "Icon": "/opt/electron-pure-admin/resources/app/dist/favicon.ico"
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    1. 通过修改nsis中guid来达到要求 guid是有规则的 不能随便填写 这个方法更灵活些 不需要每次打包的时候都修改代码 只需要进行相应配置即可
    • 首先需要新建一个generate-guid.js文件用来每次打包的时候生成一个新的guid
    function guid() { // 这是在网上找的guid生成算法
      return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
        const r = (Math.random() * 16) | 0,
          v = c == "x" ? r : (r & 0x3) | 0x8;
        return v.toString(16);
      });
    }
    
    const fs = require("fs");
    
    // 生成新的GUID
    const newGuid = guid();
    
    // 读取package.json文件
    const electronJson = JSON.parse(fs.readFileSync("electron-builder.json"));
    
    // 将新的GUID写入package.json文件
    electronJson.nsis.guid = newGuid;
    fs.writeFileSync(
      "electron-builder.json",
      JSON.stringify(electronJson, null, 2)
    );
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 其次在package.json中添加一行预打包配置代码来执行generate-guid.js文件
    	"prebuild": "node ./generate-guid.js"
    
    • 1
    • 然后运行npm run build就可以啦

    知识点:

    1. nsis中的guid指的是什么

    Electron 的 NSIS 安装程序中,GUID 是全局唯一标识符的缩写,它是一个 128 位长的数字和字母组合的字符串,用于标识软件应用程序或安装程序包的唯一性。’
    当你在 Electron 应用中使用 GUID 时,它是用来确保你的应用或安装程序的每个版本都具有唯一的标识符。这样可以避免重复安装或者覆盖安装的问题,从而确保用户在安装新的 Electron 应用版本时不会丢失之前的数据。

    1. nsis的guid可以随意赋值吗

    不可以 如果你随意赋值,可能会导致安装程序出现错误或冲突,例如覆盖安装而不是更新应用程序,或者无法正确识别已安装的应用程序版本

    1. npm run xxx的执行顺序

    npm脚本有pre和post两个钩子,例如执行npm run build的时候,会自动按照下面的顺序执行:npm run prebuild&& npm run build&& npm run postbuild。通过定义prebuild或postbuild所对应的脚本,就可以让它们在build命令的前后执行2。

  • 相关阅读:
    C#制做一个 winform下的表情选择窗口
    【 OpenGauss源码学习 —— 列存储(CStore)(四)】
    MySQL远程链接踩坑
    力扣3.无重复字符的最长子串(JavaScript版本)
    Android问题笔记 - NoSuchmethodException: could not find Fragment constructor
    算法深度解析:视频实时美颜SDK背后的技术奥秘
    每天一个设计模式之迭代器模式(Iterator Pattern)
    计算机网络之物理层
    Navicat连接postgresql时出现‘datlastsysoid does not exist‘报错的问题
    Kubernetes(k8s)使用ingress发布服务
  • 原文地址:https://blog.csdn.net/weixin_44284981/article/details/132853012