2020年9月24日撰写:
今天尝试在Electron使用Better-sqlite3,遇到了很多坑,主要还是使用sqlite3库时感觉特别不爽才硬刚的
就不多说了,直接进入正题。
安装better-sqlite3需要在Electron运行需要保证Node和Electron的ABI(NODE_MODULE_VERSION)一至;
https://nodejs.org/zh-cn/download/releases/ Node版本对照表;
我使用的nodejs版本为v12.16.3(ABI为72),electron为v9.3.1(ABI为82)以下是electron官网提供的搭配
第一步,确定安装Python2.x、vs2015或vs2017;
第二步,执行以下代码:
npm install --global windows-build-tools
#或者
cnpm install --global windows-build-tools
npm install --global windows-build-tools --vs2015
#或者
cnpm install --global windows-build-tools --vs2015
代码中出现npm和cnpm,在install过程中使用npm会比较慢,cnpm则是阿里云定制的npm版本,在国内下载速度较快。通过以下命令安装cnpm:
npm install -g cnpm --registry=https://registry.npm.taobao.org
安装node-gyp
npm install -g node-gyp
安装better-sqlite3:
npm install better-sqlite3
第三步,在项目中调用better-sqlite3:
const DB = require('better-sqlite3');
如果出现以下问题:
was compiled against a different Node.js version using
NODE_MODULE_VERSION XX. This version of Node.js requires
NODE_MODULE_VERSION XX. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
可以通过以下命令解决:
npm install --save-dev electron-rebuild
.\node_modules\.bin\electron-rebuild -f -w better-sqlite3
如果还是不行,可以试试:
cnpm rebuild --runtime=electron --target=9.3.1 --disturl=https://atom.io/download/atom-shell --abi=64
target为electron版本,精确编译对应abi的版本,过程较慢,耐心等待。
abi版本我尝试过64 和 73 对应electron 9.3.1和10.1.3
如果还是不行,可以在下面留言。