背景
本人之前用C#的winform写了一个桌面端的小软件,开发工具为visual stdio2019。用于记录时间,存储数据用的Access数据库(微软自带的,类似office三件套)。
但winform的文档太少了,有时候程序有bug看文档很难看出来,网上能找到的也都是很久之前的资料,有时候要自定义功能也难,因此最近学习了Vue+Electron准备重写。但是集成SQLite的时候遇到了问题,因此单独写了一个demo用于测试。
已将相关代码上传到GitHub:Peter-JXL/electron_sqlite3
遇到的问题概述
用Electron集成SQLite时,需要重新编译SQLite,用electron-rebuild的时候报错。
Error: Could not find any Python installation to use
但是我已确定是安装了Python的,配置了环境变量 试过重启VSCode与电脑。
python -V
Python 2.7.14
完整报错如下:
PS D:\Projects\FrontProjects\electron_sqlite3> .\node_modules\.bin\electron-rebuild.cmd
✖ Rebuild Failed
An unhandled error occurred inside electron-rebuild
node-gyp failed to rebuild 'D:\Projects\FrontProjects\electron_sqlite3\node_modules\sqlite3'.
For more information, rerun with the DEBUG environment variable set to "electron-rebuild".
Error: Could not find any Python installation to use
For more information, rerun with the DEBUG environment variable set to "electron-rebuild".
at processTicksAndRejections (node:internal/process/task_queues:94:5)
at async ModuleRebuilder.rebuildNodeGypModule (D:\Projects\FrontProjects\electron_sqlite3\node_modules\electron-rebuild\lib\src\module-rebuilder.js:94:9)
at async ModuleRebuilder.rebuild (D:\Projects\FrontProjects\electron_sqlite3\node_modules\electron-rebuild\lib\src\module-rebuilder.js:124:14)
at async Rebuilder.rebuildModuleAt (D:\Projects\FrontProjects\electron_sqlite3\node_modules\electron-rebuild\lib\src\rebuild.js:145:13)
at async Rebuilder.rebuild (D:\Projects\FrontProjects\electron_sqlite3\node_modules\electron-rebuild\lib\src\rebuild.js:108:17)
at async D:\Projects\FrontProjects\electron_sqlite3\node_modules\electron-rebuild\lib\src\cli.js:154:9
集成过程
npm install -g @vue/cli
vue --version @vue/cli 5.0.4
vue create electron_sqlite3 //用的Vue3
vue add electron-builder
npm run electron:serve --能成功运行项目
网上说要重新编译SQLite3:不重新编译会报错
Node.js 和 Electron 使用的都是 Google 的 V8 JavaScript 引擎,但却不是同一个版本,此外 sqlite 3 模块虽然接口是 JavaScript 的,但内核其实是使用 C 实现的,而且编译生成了 Node.js 的本地模块,也就是前面错误中的 node_sqlite3.node 文件。这是一个二进制文件,对 V8 引擎版本敏感。由于 sqlite 3 模块在发布时使用的是 Node.js 中的 V8 引擎,因而放在 Electron 中自然就不好使,要想在 Electron 中使用 sqlite 3 模块,就必须利用 Electron V8 引擎重新编译 sqlite 3 模块。参考Electron开发:sqlite 3 模块操作 SQLite 数据库 - 简书
看到重新编译需要安装visual stdio,本人是有的,而且直接就是用VS去开发过;
编译时需要用到node-gyp,这个工具是用Python2.7写的,所以我也卸载了原本的Python3.9,重新安装Python2.7。
开始集成SQLite3:
npm install -g node-gyp //安装完后验证:node-gyp -v:v9.0.0
npm install --save sqlite3
npm install --save-dev electron-rebuild
.\node_modules\.bin\electron-rebuild.cmd //这一步报错了
.\node_modules\.bin\electron-rebuild.cmd -v 13.0.0 //指定了Electron版本号,但还是报错
我对于前端属于刚入门没多久吧,我试过不重新编译,直接在App.vue里引入的话有166多个报错?……
<script>
import HelloWorld from "./components/HelloWorld.vue";
const sqlite3 = require('sqlite3').verbose();
console.log(sqlite3);
export default {
name: "App",
components: {
HelloWorld,
},
};
</script>
大部分报错都是Module not found,看上去都是node自身的模块,例如os,path,fs,stream等等:
Module not found: Error: Can't resolve 'os' in 'D:\Projects\FrontProjects\electron_sqlite3\node_modules\node-gyp\lib'
开发环境
Win10 64位
node: v15.13.0. 通过官网安装,没有用nvm
npm: v7.7.6
node-gyp -v:v9.0.0
Vue: 3.2.13
Electron: 13.0.0
Python:2.7,通过官网安装的,安装路径在C:\Python27,安装时选择了配置到环境变量,检查环境变量上有
尝试过的解决方法
1. 重新安装Python
看node-gyp官网里说用micro store里的Python……然而只有3以上的版本,安装完换了一个报错
nodejs/node-gyp: Node.js native addon build tool
PS D:\Projects\timerecorder> .\node_modules\.bin\electron-rebuild.cmd -v 13.0.0
⠋ Building module: sqlite3, Completed: 1在此解决方案中一次生成一个项目。若要启用并行生成,请添加“-m”开关。
⠹ Building module: sqlite3, Completed: 1 nothing.c
⠴ Building module: sqlite3, Completed: 1 win_delay_load_hook.cc
⠇ Building module: sqlite3, Completed: 1 nothing.vcxproj -> D:\Projects\timerecorder\node_modules\sqlite3\build\Release\\nothing.lib
⠋ Building module: sqlite3, Completed: 1 unpack_sqlite_dep
⠦ Building module: sqlite3, Completed: 1 sqlite3.c
⠸ Building module: sqlite3, Completed: 1 win_delay_load_hook.cc
⠧ Building module: sqlite3, Completed: 1 sqlite3.vcxproj -> D:\Projects\timerecorder\node_modules\sqlite3\build\Release\\sqlite3.lib
⠇ Building module: sqlite3, Completed: 1 backup.cc
⠸ Building module: sqlite3, Completed: 1D:\Projects\timerecorder\node_modules\sqlite3\node_modules\node-addon-api\napi-inl.h(1620,24): error C3861: “napi_object_freeze”: 找不到标识符 [D:\Projects\timerecorder\node_
modules\sqlite3\build\node_sqlite3.vcxproj]
D:\Projects\timerecorder\node_modules\sqlite3\node_modules\node-addon-api\napi-inl.h(1625,24): error C3861: “napi_object_seal”: 找不到标识符 [D:\Projects\timerecorder\node_mo
dules\sqlite3\build\node_sqlite3.vcxproj]
✖ Rebuild Failed
An unhandled error occurred inside electron-rebuild
node-gyp failed to rebuild 'D:\Projects\timerecorder\node_modules\sqlite3'.
For more information, rerun with the DEBUG environment variable set to "electron-rebuild".
Error: `C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe` failed with exit code: 1
Error: node-gyp failed to rebuild 'D:\Projects\timerecorder\node_modules\sqlite3'.
For more information, rerun with the DEBUG environment variable set to "electron-rebuild".
Error: `C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe` failed with exit code: 1
最后还是换回了Python2,也能正常进入REPL环境:
PS D:\Projects\FrontProjects\electron_sqlite3> python
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 1+1
2
2.用anaconda
用anaconda:我用anaconda的话创建了Python3和Python2的环境,但对于如何配置环境变量,使得在cmd里能直接使用不太会,因此没有继续尝试
3 测试SQLite3的问题
我新建了一个test.js文件,直接node /src/data/test.js 能正常创建test.db文件,能用sqlite studio打开:
const sqlite3 = require('sqlite3').verbose()
let db;
var fs = require('fs')
fs.exists('test.db', function(exists){
if(exists){
fs.unlinkSync('test.db');
}
db = new sqlite3.Database('test.db', sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err)=>{
if(err){
console.error(err)
}
else{
console.log('连接test.db成功');
}
let createTableSql = `create table if not exists t_products(
id integer primary key autoincrement,
product_name varchar(100) not null,
price float not null
)`;
db.run(createTableSql,function(err){
if(err){
console.error(err)
}
})
})
})
4. 重新安装SQLite3
npm install sqlite3 --build-from-source --runtime=electron --target=13.0.0 --dist-url=https://atom.io/download/electron
5 "postinstall": "electron-builder install-app-deps",
看到有的回答是在package.json里,script加上这个,然后执行 npm run postinstall
执行完后没有什么报错,也试过执行npm run rebuild,但是无论是在main.js里还是app.vue里import都会报错
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint",
"electron:build": "vue-cli-service electron:build",
"electron:serve": "vue-cli-service electron:serve",
"postinstall": "electron-builder install-app-deps",
"rebuild": "electron-rebuild -f -w sqlite3"
},
//在main.js或者app.vue里引入都会报错:
const sqlite3 = require('sqlite3').verbose();
console.log(sqlite3);
6 npm set config python python
执行后还是报错,试过
npm set config python "C:\Python27"
参考的配置方法
百度过、看了SQLite包官网,CSDN、知乎,思否,B站也都搜索了下,大部分都说是要重新编译的
Electron开发:sqlite 3 模块操作 SQLite 数据库 - 简书:主要参考这里的rebuild
在B站看到有人也是直接在app.vue里引入的,能正常集成:
关于跟朋友聊着聊着就做了个软件这件事_哔哩哔哩_bilibili
参考思否里的一个回答,里面提到了stackoverflow的回答,也和我的步骤是类似的:
用web技术做桌面应用数据本地+云端有什么成熟的架构吗? - SegmentFault 思否
试一下先设定
npm config
,然后再rebuild
: