折腾了一天终于解决了,看了很多其他博客,都是比较片面,最终出现各种问题.现自己整理一篇完整版.
方案一: (自定义编译sqlite)
环境:vs2015 ,先安装vs2015
下载地址:https://my.visualstudio.com/Downloads?q=visual%20studio%202015&wt.mc_id=o~msft~vscom~older-downloads
1: npm install --vs2015 windows-build-tools -g (这个用 vs2019会卡住,下载很慢)
2: npm install node-gyp -g
3: npm config set msvs_version 2015 --global
4: npm config set python C:\Python27\python.exe //无需配置 (第一步会自动安装python2.7,这个版本会提示太低,自己到官网下载 3.6以上版本,并配置全局变量即可)
5: node-gyp configure --module_name=node_sqlite3 --module_path=../lib/binding/electron-v14.0.0-win32-x64
6: node-gyp rebuild --target=14.0.0 --arch=x64 --target_platform=win32 --dist-url=https://atom.io/download/electron/ --module_name=node_sqlite3 --module_path=../lib/binding/electron-v14.0.0-win32-x64
(14.0.0 electron版本号, sqlite默认安装的是适合node原生环境,在electron中必须进行二次编译才能用)
在上面执行过程中也可能出现各种错误,不要慌:
q1----如果出现 缺少windows SDK8.1问题, 在控制面板找到安装的vs2015->点击修改,选择sdk安装即可.
q2---如果出现问题安装 sqlite3 (..\src\database.cc(35): error C2248: 'Napi::Env::DefaultFini': 无法访问类 'Napi::Env' 中声明的私有成员
则可能是因为sqlite安装的版本太高, 还没正式发布,可删了重装
npm install [email protected] 再进行编译即可.
q3----如果出现 找不到python问题, 就自己下载安装 并配置全局变量
方案二:
基于 electron-builder (推荐,脚手架会出现一些解决不了坑), 会自动编译,不需要自定义配置编译sqlite3这中node 原生模块,
环境: 先安装vs2015 + python3.6+
环境没问题的情况下:
npm install sqlite3 --save
npm install electron-rebuild --save
.\node_modules\.bin\electron-rebuild.cmd
即便一步步来,也有可能出错,根据log自行 搜索吧
{ "name": "CTYTHApp", "author": "j8", "version": "2.0.4", "main": "main.js", "scripts": { "rebuild": "electron-rebuild -f -w sqlite3", "electron": "electron . --inspect", "build": "electron-builder --win --x64", "uos": "electron-builder build --arm64", "build:l": "electron-builder --linux --arm64", "postinstall": "install-app-deps" }, "build": { "productName": "CTYTHApp", "appId": "com.htsz.ctyth", "electronVersion": "4.1.4", "copyright": "j8", "asar": false, "directories": { "output": "dist" }, "publish": [ { "provider": "generic", "url": "http://192.168.0.242/download/upgrade/ctyth" } ], "win": { "icon": "../src/assets/app/desktop.ico", "requestedExecutionLevel": "highestAvailable", "target": [ { "target": "nsis", "arch": [ "x64" ] } ] }, "linux": { "target": { "target": "dir", "arch": "arm64" }, "icon": "../src/assets/app/desktop.png" }, "afterPack": "AfterPackHook.js", "nsis": { "oneClick": false, "allowElevation": true, "allowToChangeInstallationDirectory": true, "installerIcon": "../src/assets/app/logo.ico", "uninstallerIcon": "../src/assets/app/uninstall.ico", "installerHeaderIcon": "../src/assets/app/logo.ico", "createDesktopShortcut": true, "createStartMenuShortcut": true, "shortcutName": "j8", "include": "../build/znfz.nsi" } }, "dependencies": { "archiver": "^3.0.0", "bluebird": "^3.5.2", "cmd": "^0.1.0", "compressing": "^1.5.1", "electron-updater": "4.0.6", "image-base64": "^1.0.2", "lodash": "^4.17.11", "mkdirp": "^0.5.1", "qs": "^6.5.2", "request": "^2.88.0", "split.js": "^1.6.2", "sqlite3": "^5.0.0", "stream-to-blob": "^1.0.1", "xml2js": "^0.4.19" }, "devDependencies": { "child_process": "^1.0.2", "electron": "^4.1.4", "electron-builder": "^20.39.0", "electron-rebuild": "^1.11.0" } }
electron-rebuild 参数:
Options:
-h, --help Show help [boolean]
-v, --version The version of Electron to build against
-f, --force Force rebuilding modules, even if we would skip
it otherwise
-a, --arch Override the target architecture to something
other than your system's
-m, --module-dir The path to the app directory to rebuild
-w, --which-module A specific module to build, or comma separated
list of modules. Modules will only be rebuilt if they
also match the types of dependencies being rebuilt
(see --types).
-e, --electron-prebuilt-dir The path to electron-prebuilt
-d, --dist-url Custom header tarball URL
-t, --types The types of dependencies to rebuild. Comma
separated list of "prod", "dev" and "optional".
Default is "prod,optional"
-p, --parallel Rebuild in parallel, this is enabled by default
on macOS and Linux
-s, --sequential Rebuild modules sequentially, this is enabled by
default on Windows
-o, --only Only build specified module, or comma separated
list of modules. All others are ignored.
-b, --debug Build debug version of modules
--prebuild-tag-prefix GitHub tag prefix passed to prebuild-install.
Default is "v"