版本信息
node@8.11.0(x86版本)
node-gyp@3.8.0
win32 ia32 win10.0.18362(win10 64位)
electron@4.0.0
ffi@2.3.0
ref@1.3.5
ref-array@1.2.0
ref-struct@1.1.0
vs 2015(DLL WIN32)
puthon 2.7.15
安装流程
- 本示例是32位下开发,请自行下载32位NODEJS安装,点击下一步,修改安装位置,基本是一步到位
- 修改npm的全局包以及缓存路径(自行新建需要的文件夹)
npm config set cache "<path>\npm-cache" #设置缓存路径 (绝对路径)
npm config set prefix "<path>\npm_global"#设置全局包路径 (绝对路径)
npm config set msvs_version 2015 #设置vs版本
npm config set registry https://registry.npm.taobao.org #设置国内源(对npm无效,感觉没什么用)
npm config set strict-ssl false #源不加密
- 设置全局包环境变量
<path>\node_global; #环境变量PATH添加路径 (绝对路径)
- 可选安装cnpm,建议少用
npm install -g cnpm --registry=https://registry.npm.taobao.org
- 安装win编译工具(找到Windows PowerShell ,右键使用管理身份运行,打开后在其中运行以下命令)
npm install -g windows-build-tools --vs2015
- 安装 node-gyp
npm install -g node-gyp@3.8.0 #-g 表示全局安装
- 安装ffi(项目目录下安装)
npm init #创建package.json文件(修改后的完整版见下面示例)
npm install -S ffi@2.3.0 #-S 加入package.json的dependencies依赖
- 安装ref-array(可选安装,有用到就装,但要记得使用electron重编,否则会报错,编译方式见下面示例)
npm install -g ref-array@1.2.0 #其它几个ref包已在ffi中依赖安装了
- 重新编译上面几个安装库(为了保证与electron兼容)
cd <program path>\node_modules\ffi
node-gyp rebuild --runtime=electron --target=4.0.0 --dist-url=https://npm.taobao.org/mirrors/atom-shell --abi=69 --msvs_version=2015 #注意指明国内源(使用国外就看运气了--disturl=https://atom.io/download/atom-shell)
cd <program path>\node_modules\ref
node-gyp rebuild --runtime=electron --target=4.0.0 --dist-url=https://npm.taobao.org/mirrors/atom-shell --abi=69 --msvs_version=2015
cd <program path>\node_modules\ref-array #如果缺少binding.gyp以及src文件夹,请将ref下的拷贝过来即可
node-gyp rebuild --runtime=electron --target=4.0.0 --dist-url=https://npm.taobao.org/mirrors/atom-shell --abi=69 --msvs_version=2015
- 安装 electron(建议在项目目录下安装,这样支持不同的项目安装不同的electron版本)
cnpm install -D electron@x.x.x #这个用cnpm安装好像没什么影响,npm安装太慢 -D 加入package.json的devDependencies依赖
- 安装好后项目下的package.json示例
{
"private": true,
"name": "demo",
"main": "main.js",
"dependencies": {
"ffi": "^2.3.0",
"ref": "^1.3.5",
"ref-array": "^1.2.0",
"ref-struct": "^1.1.0"
},
"scripts": {
"start": "electron ."
},
"devDependencies": {
"electron": "^4.0.0"
}
}
- 安装vscode,最新版本就好
- 设置vscode的 launch.json 参数(具体操作自行搜索)
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Electron Main",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron",//如果是全局安装,使用全局路径
"program": "${workspaceFolder}/main.js",//启动主文件
"args" : ["."],
"protocol": "inspector"//此协议支持断点
},
]
}
- 加载项目下的DLL,请在代码中添加如下代码
var kernel32 = ffi.Library('kernel32', {
'SetDllDirectoryA': ['bool', ['string']]
});
kernel32.SetDllDirectoryA('<path>\\lib'); //库路径
- 编译通过后打包请自行参考其它资料
最后
这篇文章是花费好几天总结出来的血泪史,参考了很多资料(见下面,这里特别感谢下,虽然不全,但很有启发),如有问题,请及时指出,有疑问可留言,一起探讨!
参考资料:
https://blog.csdn.net/cut001/article/details/68922780/
https://blog.csdn.net/ruyulin/article/details/78885246
https://blog.csdn.net/wang839305939/article/details/83780789
https://electronjs.org/docs
https://www.npmjs.com/package/node-gyp
http://luajit.org/index.html