Skip to content

Electron 常见错误和解决方案

在 package.json 文件中,postinstall 是什么意思

在 package.json 文件中,postinstall 是一个特殊的脚本钩子(script hook),它定义了在 npm install 命令执行完成后自动运行的脚本。

当您运行 npm install 安装依赖包时,postinstall 脚本会在安装完成后自动触发。这使得您可以在安装完成后执行一些额外的操作或任务,例如构建应用程序、安装依赖项的二进制文件、运行初始化脚本等。

在给定的示例中,postinstall 脚本包含了 electron-builder install-app-deps 命令。这个命令是由 electron-builder 提供的一个工具,用于安装应用程序的运行时依赖项。通过在 postinstall 脚本中调用该命令,可以确保在安装依赖包后,自动安装所需的应用程序依赖项。

因此,在这个特定的配置中,当您运行 npm install 命令时,安装依赖包完成后,会自动执行 electron-builder install-app-deps 命令来安装应用程序的运行时依赖项。


package.json

json
"scripts": {
  "electron:build:x64": "npm run postinstall && vue-cli-service electron:build",
  "electron:build:ia32": "npm run postinstall:ia32 && vue-cli-service electron:build",
  "electron:serve": "vue-cli-service electron:serve",
  "postinstall:help": "electron-builder install-app-deps --help",
  "postinstall:ia32": "electron-builder install-app-deps --arch=ia32",
  "postinstall": "electron-builder install-app-deps",
  "postuninstall": "electron-builder install-app-deps",
  "rebuild": "electron-rebuild",
},

当打包 ia32 应用的时候, 需要手动执行一下 electron:build:ia32, 此时会自动把原生模块构建出符合 ia32 位的可执行文件

bash
npm run electron:build:ia32

当打包 x64 应用的时候, 需要手动执行一下 electron:build:x64, 此时会自动把原生模块构建出符合 x64 位的可执行文件

bash
npm run electron:build:x64

npx electron-rebuild 出现下面错误

bash
Building module: sqlite3, Completed: 2gyp: name 'openssl_fips' is not defined while evaluating condition 'openssl_fips != ""' in binding.gyp while trying to load binding.gyp
Error: `gyp` failed with exit code: 1
at ChildProcess.onCpExit (E:\electron-ycb\node_modules\@electron\rebuild\node_modules\node-gyp\lib\configure.js:325:16)
at ChildProcess.emit (node:events:513:28)
at ChildProcess.\_handle.onexit (node:internal/child_process:291:12)

 Rebuild Failed

An unhandled error occurred inside electron-rebuild
node-gyp failed to rebuild 'E:\electron-ycb\node_modules\sqlite3'

Error: node-gyp failed to rebuild 'E:\electron-ycb\node_modules\sqlite3'
at ChildProcess.<anonymous> (E:\electron-ycb\node_modules\@electron\rebuild\lib\module-type\node-gyp\node-gyp.js:118:24)
at ChildProcess.emit (node:events:513:28)
at ChildProcess.\_handle.onexit (node:internal/child_process:291:12)
npm notice
npm notice New minor version of npm available! 9.5.0 -> 9.6.7
npm notice Changelog: https://github.com/npm/cli/releases/tag/v9.6.7
npm notice Run npm install -g npm@9.6.7 to update!
npm notice

解决办法:

  1. 安装 electron-rebuild 模块,用于重新编译 sqlite3 模块以适应不同的 Electron 架构:
bash
npm install electron-rebuild --save-dev
  1. 清除 npm 缓存。运行以下命令来清除 npm 缓存:
bash
npm cache clean --force
  1. 删除 node_modules 目录和 package-lock.json(或 yarn.lock)文件。然后再次运行以下命令来重新安装依赖项:
npm install
  1. 尝试手动重新构建 sqlite3 模块。运行以下命令:
bash
node-gyp rebuild --target=YOUR_ELECTRON_VERSION --arch=YOUR_ARCH
node-gyp rebuild --target=13.0.0 --arch=ia32
  1. 在项目的根目录下,运行以下命令来执行 electron-rebuild:
bash
npx electron-rebuild

这将根据您的项目配置重新编译 sqlite3 模块以适应当前使用的 Electron 版本和架构。

请注意,您在运行 electron-rebuild 命令时需要确保已经设置了正确的环境变量,以便使用 Electron 相关的工具和库。

以上步骤将帮助您在项目中安装并编译适用于 ia32 和 x64 架构的 sqlite3 模块。


在 Electron 项目中支持 SQLite3 的 ia32 架构

  1. 切换到 sqlite3 目录
powershell
cd node_modules\sqlite3
  1. 运行 node-pre-gyp install --fallback-to-build --target_arch=ia32

上述命令中的 --target_arch=ia32 指定了要编译为 32 位的架构。如果你希望编译为 64 位的架构,可以将 --target_arch 设置为 x64。

powershell
PS E:\electron-ycb\node_modules\sqlite3> node-pre-gyp install --fallback-to-build --target_arch=ia32
node-pre-gyp info it worked if it ends with ok
node-pre-gyp info using node-pre-gyp@1.0.10
node-pre-gyp info using node@18.15.0 | win32 | x64
node-pre-gyp info check checked for "E:\electron-ycb\node_modules\sqlite3\lib\binding\napi-v6-win32-unknown-ia32\node_sqlite3.node" (not found)
node-pre-gyp http GET https://github.com/TryGhost/node-sqlite3/releases/download/v5.1.6/napi-v6-win32-unknown-ia32.tar.gz
node-pre-gyp info install unpacking napi-v6-win32-unknown-ia32/node_sqlite3.node
node-pre-gyp info extracted file count: 1
[sqlite3] Success: "E:\electron-ycb\node_modules\sqlite3\lib\binding\napi-v6-win32-unknown-ia32\node_sqlite3.node" is installed via remote
node-pre-gyp info ok
PS E:\electron-ycb\node_modules\sqlite3>

执行完成后,在 node_modules\sqlite3\lib\binding\napi-v6-win32-unknown-ia32 会有这个目录生成

在 Electron 项目中支持 ffi-napi 的 ia32 架构

powershell
PS E:\electron-ycb> cd node_modules/ffi-napi
powershell
PS E:\electron-ycb\node_modules\ffi-napi> node-gyp rebuild --arch=ia32
gyp info it worked if it ends with ok
gyp info using node-gyp@9.3.1
gyp info using node@18.15.0 | win32 | x64
gyp info find Python using Python version 3.9.12 found at "D:\tools\python3\python.exe"
gyp info find VS using VS2022 (17.5.33516.290) found at:
gyp info find VS "E:\vs2022"
gyp info find VS run with --verbose for detailed information
gyp info spawn D:\tools\python3\python.exe
gyp info spawn args [
gyp info spawn args   'D:\\tools\\nvm\\v18.15.0\\node_modules\\node-gyp\\gyp\\gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'msvs',
gyp info spawn args   '-I',
gyp info spawn args   'E:\\electron-ycb\\node_modules\\ffi-napi\\build\\config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'D:\\tools\\nvm\\v18.15.0\\node_modules\\node-gyp\\addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'C:\\Users\\Administrator\\AppData\\Local\\node-gyp\\Cache\\18.15.0\\include\\node\\common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=C:\\Users\\Administrator\\AppData\\Local\\node-gyp\\Cache\\18.15.0',
gyp info spawn args   '-Dnode_gyp_dir=D:\\tools\\nvm\\v18.15.0\\node_modules\\node-gyp',
gyp info spawn args   '-Dnode_lib_file=C:\\\\Users\\\\Administrator\\\\AppData\\\\Local\\\\node-gyp\\\\Cache\\\\18.15.0\\\\<(target_arch)\\\\node.lib',
gyp info spawn args   '-Dmodule_root_dir=E:\\electron-ycb\\node_modules\\ffi-napi',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'E:\\electron-ycb\\node_modules\\ffi-napi\\build',
gyp info spawn args   '-Goutput_dir=.'
gyp info spawn args ]
gyp info spawn E:\vs2022\MSBuild\Current\Bin\MSBuild.exe
gyp info spawn args [
gyp info spawn args   'build/binding.sln',
gyp info spawn args   '/clp:Verbosity=minimal',
gyp info spawn args   '/nologo',
gyp info spawn args   '/p:Configuration=Release;Platform=Win32'
gyp info spawn args ]
在此解决方案中一次生成一个项目。若要启用并行生成,请添加“-m”开关。

  nothing.c
  win_delay_load_hook.cc
  nothing.vcxproj -> E:\electron-ycb\node_modules\ffi-napi\build\Release\\nothing.lib
  Preprocessing assembly file ..\..\..\deps\libffi\src\x86\sysv_intel.preasm
  sysv_intel.preasm
  Building assembly file Release\obj\ffi\\sysv_intel.asm
   Assembling: Release\obj\ffi\sysv_intel.asm
  prep_cif.c
  types.c
  raw_api.c
  java_raw_api.c
  closures.c
  ffi.c
  win_delay_load_hook.cc
  ffi.vcxproj -> E:\electron-ycb\node_modules\ffi-napi\build\Release\\libffi.lib
  ffi.cc
  callback_info.cc
  threaded_callback_invokation.cc
  win32-dlfcn.cc
  win_delay_load_hook.cc
  正在生成代码
  Previous IPDB not found, fall back to full compilation.
  All 526 functions were compiled because no usable IPDB/IOBJ from previous compilation was found.
  已完成代码的生成
  ffi_bindings.vcxproj -> E:\electron-ycb\node_modules\ffi-napi\build\Release\\ffi_bindings.node
gyp info ok

重新打包即可

bash
npm run electron:build