记录electron引用dll的BUG历程

416 阅读3分钟

前言

最近接了一个做收银端的项目,计划使用electron来进行制作。过程中遇到了很多神奇的问题,在这里记录一下。

安装问题

安装node.js的时候,在执行到这一步的时候一定要勾选此选框,让node自动安装windows开发下所需的支持库。

image.png

如果是最新版的node,默认安装的应该是python3+vs2017(这里可以选择手动安装2015)

一定要安装32位的node

安装后的配置问题

首先可以配置系统环境变量

//在Path中添加cl.exe所在文件夹路径。若未找到,直接VS的安装目录下搜索cl.exe即可
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x86

//在系统变量中新建变量INCLUDE,添加cl.exe的包含目录
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\include


//在系统变量中新建变量LIB,添加cl.exe的库目录
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x86

我是根据他人文章搜索的这个结果,但是好像并没有什么用,这一步可以忽略。

手动安装python2

此时,去寻找python2的安装包,安装python2,

记得全局安装node-gyp

npm i node-gyp -g

npmrc配置文件

node_gyp=C:\Users\59654\AppData\Roaming\npm\node_modules\node-gyp\bin\node-gyp.js
phantomjs_cdnurl=https://npm.taobao.org/mirrors/phantomjs
chromedriver_cdnurl=https://npm.taobao.org/mirrors/chromedriver
operadriver_cdnurl=https://npm.taobao.org/mirrors/operadriver
drivers.chrome.baseURL=https://npm.taobao.org/mirrors/chromedriver
drivers.ie.baseURL=https://npm.taobao.org/mirrors/selenium
sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
PUPPETEER_DOWNLOAD_HOST=https://npm.taobao.org/mirrors
ELECTRON_MIRROR=https://npm.taobao.org/mirrors/electron/
electron_builder_binaries_mirror=https://npm.taobao.org/mirrors/electron-builder-binaries/
SQLITE3_BINARY_SITE=https://npm.taobao.org/mirrors/sqlite3
;这里的路径请自行修改,因为我是默认首选的python2.7
python=C:\Python27\python.exe
;这里版本请自行修改
msvs_version=2017 
registry=https://registry.npm.taobao.org
disturl=https://npm.taobao.org/dist
chromedriver-cdnurl=https://npm.taobao.org/mirrors/chromedriver
couchbase-binary-host-mirror=https://npm.taobao.org/mirrors/couchbase/v{version}
debug-binary-host-mirror=https://npm.taobao.org/mirrors/node-inspector
electron-mirror=https://npm.taobao.org/mirrors/electron/
flow-bin-binary-host-mirror=https://npm.taobao.org/mirrors/flow/v
fse-binary-host-mirror=https://npm.taobao.org/mirrors/fsevents
fuse-bindings-binary-host-mirror=https://npm.taobao.org/mirrors/fuse-bindings/v{version}
git4win-mirror=https://npm.taobao.org/mirrors/git-for-windows
gl-binary-host-mirror=https://npm.taobao.org/mirrors/gl/v{version}
grpc-node-binary-host-mirror=https://npm.taobao.org/mirrors
hackrf-binary-host-mirror=https://npm.taobao.org/mirrors/hackrf/v{version}
leveldown-binary-host-mirror=https://npm.taobao.org/mirrors/leveldown/v{version}
leveldown-hyper-binary-host-mirror=https://npm.taobao.org/mirrors/leveldown-hyper/v{version}
mknod-binary-host-mirror=https://npm.taobao.org/mirrors/mknod/v{version}
node-sqlite3-binary-host-mirror=https://npm.taobao.org/mirrors
node-tk5-binary-host-mirror=https://npm.taobao.org/mirrors/node-tk5/v{version}
nodegit-binary-host-mirror=https://npm.taobao.org/mirrors/nodegit/v{version}/
operadriver-cdnurl=https://npm.taobao.org/mirrors/operadriver
phantomjs-cdnurl=https://npm.taobao.org/mirrors/phantomjs
profiler-binary-host-mirror=https://npm.taobao.org/mirrors/node-inspector/
puppeteer-download-host=https://npm.taobao.org/mirrors
python-mirror=https://npm.taobao.org/mirrors/python
rabin-binary-host-mirror=https://npm.taobao.org/mirrors/rabin/v{version}
sass-binary-site=https://npm.taobao.org/mirrors/node-sass
sodium-prebuilt-binary-host-mirror=https://npm.taobao.org/mirrors/sodium-prebuilt/v{version}
sqlite3-binary-site=https://npm.taobao.org/mirrors/sqlite3
utf-8-validate-binary-host-mirror=https://npm.taobao.org/mirrors/utf-8-validate/v{version}
utp-native-binary-host-mirror=https://npm.taobao.org/mirrors/utp-native/v{version}
zmq-prebuilt-binary-host-mirror=https://npm.taobao.org/mirrors/zmq-prebuilt/v{version}
home=https://npm.taobao.org

electron-rebuild使用过程中奇怪的BUG

因为我引用了ffi-napi这个模块,此模块需要electron-rebuild来进行重新编译,否则无法正常使用,会报错Error: A dynamic link library (DLL) initialization routine failed,此时我根据文档执行electron-rebuild时,出现报错信息如下

Could not find any Visual Studio installation to use

我设置的msvs_version版本完全没问题,所以不知道是什么情况

补丁方法解决此BUG

对此,我使用patch-package模块来处理这个问题

npm i patch-package

并且在package.json中加入

scripts:{
...,
"postinstall": "patch-package",
"install": "electron-rebuild" //这个是你在安装扩展包的时候,自动执行一下electron-rebuild命令
}

具体的修改文件如下

node_modules/node-gyp/lib/find-visualstudio.js中的findVisualStudio方法

 
 function VisualStudioFinder (nodeSemver, configMsvsVersion, callback) {
   this.nodeSemver = nodeSemver
-  this.configMsvsVersion = configMsvsVersion // 这一行是删除的
+  this.configMsvsVersion = '2017' // 此处为你系统的Visual Studio版本号
   this.callback = callback
   this.errorLog = []
   this.validVersions = []
}

修改完毕后执行npx patch-package node-gyp即可