element-forge在Linux Centos中打包构建时遇到的异常问题解决方案

环境:
Linux CentOS8 x64
electron:27.1.0
electron-forge: 7.1.0

electron dev依赖包
"devDependencies": {
"@electron-forge/cli": "^7.1.0",
"@electron-forge/maker-deb": "^7.1.0",
"@electron-forge/maker-rpm": "^7.1.0",
"@electron-forge/maker-squirrel": "^7.1.0",
"@electron-forge/maker-zip": "^7.1.0",
"@electron-forge/plugin-auto-unpack-natives": "^7.1.0",
"@electron/rebuild": "^3.3.0",
"@vitejs/plugin-vue": "3.1.0",
"electron": "^27.1.0"
}

构建不同架构的软件包

如果你想构建不同架构的软件包,需要指定arch命令行参数。
命令:npm run make -- --arch="arm64,x64"
electron-forge cli关于arch参数的说明及使用方式

异常记录

Cannot make for rpm, the following external binaries need to be installed: rpmbuild

An unhandled rejection has occurred inside Forge:
Error: Cannot make for rpm, the following external binaries need to be installed: rpmbuild

出现这个错误,代表你的系统上缺少环境依赖包,需要进行安装。
安装命令:dnf install rpm-build.x86_64;有的异常给出的并不是包名,
而是命令,所以这个时候就需要查询这个命令是在哪个包中再进行安装。
也可以通过dnf search <包名>命令进行尝试性的检索,看看能不能找到。

An unhandled rejection has occurred inside Forge:
Error: /tmp/electron-packager/tmp-9UkTiG/resources/app/node_modules/@serialport/bindings-cpp/build/node_gyp_bins/python3: file "../../../../../usr/libexec/platform-python3.6" links out of the package

出现这个错误是一个bug,这里需要修改forge.config.js文件或等同的配置项内容,添加hooks项。
修改后的文件内容如下:

const path = require('path')
const fs = require('node:fs/promises');

module.exports = {
  packagerConfig: {
    asar: true,
    ...
  },
  makers: [
    ...
  ],
  plugins: [
    ...
  ],
  hooks: {
    packageAfterPrune: async (_config, buildPath) => {
      const gypPath = path.join(
          buildPath,
          'node_modules',
          '@serialport',
          'bindings-cpp',
          'build',
          'node_gyp_bins'
      );
      await fs.rm(gypPath, {recursive: true, force: true});
    }
  }
};

注意:其他配置内容保留你自己的即可。

'@serialport','bindings-cpp'这里是我这边的模块名称,替换你自己的模块命令。

导致这个错误的原因相关的文档记录:
node_gyp_bins导致下游包构建失败
在 Mac 上启用 asar 时,Electron forge 会抛出“.../python3.8”链接超出包”错误

Cannot make for deb, the following external binaries need to be installed: dpkg, fakeroot

An unhandled rejection has occurred inside Forge:
Error: Cannot make for deb, the following external binaries need to be installed: dpkg, fakeroot
at MakerDeb.ensureExternalBinariesExist

如果你构建deb格式的包,那么可能就会出现上面这种错误,还是需要安装这2个依赖包。

在安装之前先执行yum -y install epel-release添加扩展仓库包。

安装命令:yum install dpkg-1.20.9-4.el8.x86_64

自己确认是否需要额外安装的包:dpkg-devel.x86_64 dpkg-dev.noarch

fakeroot安装命令:dnf install fakeroot.x86_64 fakeroot-libs.x86_64

Package name must start with an ASCII number or letter

Error: Package name must start with an ASCII number or letter
at DebianInstaller.sanitizeName

这里是构建名称的强制条件,我这边没有通过forge.config.js文件单独配置名称,所以我这里修改的是package.json文件的内容。

  "name": "test-exec-name",

You may need to re-bundle the app using Electron Packager's "executableName" option.

An unhandled rejection has occurred inside Forge:
Error: could not find the Electron app binary at "/path1/path2/...". You may need to re-bundle the app using Electron Packager's "executableName" option.

这里也就是根据你提供的name名称,并未找到二进制文件目录,我这里是使用package.json配置的name名称,其他的配置文件会继承这个属性的值,所以,要不你就在marker配置项那里单独指定名称,要不你就统一名称,例如:name和productName名称保持一致。
这是关于deb marker配置项:https://js.electronforge.io/interfaces/_electron_forge_maker_deb.InternalOptions.MakerDebConfigOptions.html

error: License field must be present in package: (main package)

这个错误很明确,就是你没有声明许可协议。在package.json包中添加"license": "GPL"即可,协议的值根据你自己的需要进行修改。

最后的最后,你可以放心执行npm run make进行构建了,我这边遇到的错误都已放在这里了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章