Win10+NodeJS+Electron+ffi+DLL+VSCode安裝配置編譯流程及要點

版本信息

[email protected](x86版本)
[email protected]
win32 ia32 win10.0.18362(win10 64位)
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
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

發佈了37 篇原創文章 · 獲贊 11 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章