Electron项目中安装better-sqlite3问题记录

项目node版本:20.16.0、electron版本:31.0.2、包管理器pnpm

解决办法总结在最底下,下面是个人不断踩坑的问题记录与解决办法。


1、安装better-sqlite3

pnpm i -S better-sqlites

报错: 

node_modules/.pnpm/better-sqlite3@11.2.1/node_modules/better-sqlite3: Running install script, failed in 10.8s

.../node_modules/better-sqlite3 install$ prebuild-install || node-gyp rebuild --release

│ prebuild-install warn install socket hang up

│ gyp info it worked if it ends with ok

│ gyp info using node-gyp@9.4.1

│ gyp info using node@20.16.0 | win32 | x64

│ gyp info find Python using Python version 3.11.9 found at "D:\Python\python3.exe"

│ gyp ERR! find VS

│ gyp ERR! find VS msvs_version not set from command line or npm config

│ gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt

│ gyp ERR! find VS checking VS2022 (17.6.33829.357) found at:

│ gyp ERR! find VS "D:\Microsoft Visual Studio\2022\Professional"

│ gyp ERR! find VS - found "Visual Studio C++ core features"

│ gyp ERR! find VS - missing any VC++ toolset

│ gyp ERR! find VS could not find a version of Visual Studio 2017 or newer to use

│ gyp ERR! find VS not looking for VS2015 as it is only supported up to Node.js 18

│ gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8

│ gyp ERR! find VS

│ gyp ERR! find VS **************************************************************

│ gyp ERR! find VS You need to install the latest version of Visual Studio

│ gyp ERR! find VS including the "Desktop development with C++" workload.

│ gyp ERR! find VS For more information consult the documentation at:

│ gyp ERR! find VS GitHub - nodejs/node-gyp: Node.js native addon build tool

│ gyp ERR! find VS **************************************************************

│ gyp ERR! find VS

│ gyp ERR! configure error

│ gyp ERR! stack Error: Could not find any Visual Studio installation to use

│ gyp ERR! stack     at VisualStudioFinder.fail (E:\Fast\Electron_Vite\FastCSGIS1.0\node_modules\.pnpm\node-gyp@9.4.1\node_modules\node-gyp\lib\find-visualstudio.js:122:47)

│ gyp ERR! stack     at E:\Fast\Electron_Vite\FastCSGIS1.0\node_modules\.pnpm\node-gyp@9.4.1\node_modules\node-gyp\lib\find-visualstudio.js:75:16

│ gyp ERR! stack     at VisualStudioFinder.findVisualStudio2013 (E:\Fast\Electron_Vite\FastCSGIS1.0\node_modules\.pnpm\node-gyp@9.4.1\node_modules\node-gyp\lib\find-visualstudio.js:38…    

│ gyp ERR! stack     at E:\Fast\Electron_Vite\FastCSGIS1.0\node_modules\.pnpm\node-gyp@9.4.1\node_modules\node-gyp\lib\find-visualstudio.js:71:14

│ gyp ERR! stack     at VisualStudioFinder.findVisualStudio2015 (E:\Fast\Electron_Vite\FastCSGIS1.0\node_modules\.pnpm\node-gyp@9.4.1\node_modules\node-gyp\lib\find-visualstudio.js:36…    

│ gyp ERR! stack     at E:\Fast\Electron_Vite\FastCSGIS1.0\node_modules\.pnpm\node-gyp@9.4.1\node_modules\node-gyp\lib\find-visualstudio.js:67:12

│ gyp ERR! stack     at VisualStudioFinder.parseData (E:\Fast\Electron_Vite\FastCSGIS1.0\node_modules\.pnpm\node-gyp@9.4.1\node_modules\node-gyp\lib\find-visualstudio.js:237:5)

│ gyp ERR! stack     at E:\Fast\Electron_Vite\FastCSGIS1.0\node_modules\.pnpm\node-gyp@9.4.1\node_modules\node-gyp\lib\find-visualstudio.js:143:14

│ gyp ERR! stack     at ChildProcess.exithandler (node:child_process:414:7)

│ gyp ERR! stack     at ChildProcess.emit (node:events:519:28)

│ gyp ERR! stack     at maybeClose (node:internal/child_process:1105:16)

│ gyp ERR! stack     at ChildProcess._handle.onexit (node:internal/child_process:305:5)

│ gyp ERR! System Windows_NT 10.0.22631

│ gyp ERR! command "D:\\nodejs\\node.exe" "E:\\Fast\\Electron_Vite\\FastCSGIS1.0\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--release"

│ gyp ERR! cwd E:\Fast\Electron_Vite\FastCSGIS1.0\node_modules\.pnpm\better-sqlite3@11.2.1\node_modules\better-sqlite3

│ gyp ERR! node -v v20.16.0

│ gyp ERR! node-gyp -v v9.4.1

│ gyp ERR! not ok

└─ Failed in 10.8s at E:\Fast\Electron_Vite\FastCSGIS1.0\node_modules\.pnpm\better-sqlite3@11.2.1\node_modules\better-sqlite3

 ELIFECYCLE  Command failed with exit code 1.

报错原因:可能是缺少“Visual Studio C++ core features”“ "Desktop development with C++" 

一通搜索以及各种尝试,下面是绕来绕去的解决步骤。

解决步骤

1、安装windows-build-tools,目的是为了自动给安装上一些缺少的构建工具

pnpm i -g  windows-build-tools

报错:

忘记截图了,大概就是提示无法下载 “python-2.7.15.amd64.msi”和“vs_BuildTools.exe”。看了一下windows-build-tools的包信息,应该是镜像源地址有问题,就直接去全局包的位置里面更改了一下

继续执行上面这个命令,终于下载成功,可以在windows-build-tools下载路径里面找到下载的两个文件。但是自动安装失败,自己没截图,网上找到一个一样的报错,这个下面的解决办法大概就是说什么node版本问题啥的。我就没理,想着要不要手动安装得了。

2、根据最开始的报错以及“windows-build-tools”下载内容,应该是需要安装python和visual studio “c++”桌面开发什么的。

(1)安装python

由于我本地已经安装过了python3,不确定这东西是否一定是需要上面这个python2,于是就再手动安装了上面下载下来这个。python双版本共存安装(保姆级教程)_python安装两个版本-CSDN博客

如果原本就没有的话,直接安装配置好就行。python安装配置略~

(2)visual studio “c++”桌面开发

双击运行“vs_BuildTools.exe”

猜测应该是要安装这个visual c++生成工具。但是由于本机也安装过visual stutio2022,想着应该可能也有这个工具。于是就是打开了这个的visual studio installer ,选择再增加安装下面红框内容。如果没有的,可以就直接上面这个安装一下,但是我没测试。

安装好之后,重启电脑,再执行命令安装better-sqlite3,安装成功。至此,better-sqlite3安装成功。

2、better-sqlite3运行报错

报错信息:

Error: The module '\\?\E:\Fast\Electron_Vite\FastCSGIS1.0\node_modules\.pnpm\better-sqlite3@11.2.1\node_modules\better-sqlite3\build\Release\better_sqlite3.node'

was compiled against a different Node.js version using

NODE_MODULE_VERSION 115. This version of Node.js requires

NODE_MODULE_VERSION 125. Please try re-compiling or re-installing

the module (for instance, using `npm rebuild` or `npm install`).

    at process.func [as dlopen] (node:electron/js2c/node_init:2:2559)

    at Module._extensions..node (node:internal/modules/cjs/loader:1470:18)

    at Object.func [as .node] (node:electron/js2c/node_init:2:2559)

    at Module.load (node:internal/modules/cjs/loader:1215:32)

    at Module._load (node:internal/modules/cjs/loader:1031:12)

    at c._load (node:electron/js2c/node_init:2:17025)

    at Module.require (node:internal/modules/cjs/loader:1240:19)

    at require (node:internal/modules/helpers:179:18)

    at bindings (E:\Fast\Electron_Vite\FastCSGIS1.0\node_modules\.pnpm\bindings@1.5.0\node_modules\bindings\bindings.js:112:48)

    at new Database (E:\Fast\Electron_Vite\FastCSGIS1.0\node_modules\.pnpm\better-sqlite3@11.2.1\node_modules\better-sqlite3\lib\database.js:48:64)

报错原因:需要保证Node和Electron的ABI(NODE_MODULE_VERSION)一致。

这个问题其实是我搜better-sqlite3安装失败相关解决方法,一开始就出现的。

解决步骤

1、安装electron-rebuild

pnpm i -D @electron/rebuild

2、脚本配置

"rebuild": "electron-rebuild -f -w better-sqlite3"

在package.json的scripts内添加上面脚本

3、执行该脚本,提示“Rebuild Complete”

至此,就可以正常使用better-sqlite3

总结:

1、better-sqlite3的安装需要提前先安装好一些构建工具:python和c++ 桌面开发。可以手动安装,也可以用windows-build-tools自动安装。反正我觉得windows-build-tools挺坑。至于还缺不缺少其他,此次better-sqlite3的安装使用并没有提示,有坑再填吧。

2、better-sqlite3需要保证项目Node版本和Electron的ABI(NODE_MODULE_VERSION)一致。在不更改项目node版本的情况下,可以用electron-rebuild 来重建 Electron 项目中本地 Node.js 模块的。

参考了很多blog,有用的没用的,感谢!有空再贴上来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值