- node 編譯 c++ 擴(kuò)展的時(shí)候使用的編譯工具
- depend on: python 2.7
開發(fā)一個 windows 小工具,工作 pc 是 ubuntu, 為了省事,選擇用 electron,打算開發(fā)好了直接到 windows 上打包(ubuntu 上打 win 包還在研究中...)
本以為網(wǎng)上資料一大堆,找個能用的抄抄就好了,結(jié)果因?yàn)?node-gyp 編譯問題扯了一天半還沒解決...
大部分對 node-gyp 在 win 上的安裝教程都是抄來抄去,其實(shí)就是同一篇文章,而且還不頂用, 猜測 npm 安裝 windows-build-tools 可能需要參數(shù)輔助或其它支持...
實(shí)在受不了,只好花時(shí)間研究下 win 環(huán)境的 node-gyp 編譯工具如何正確安裝使用,在此作個記錄
分 2 種情況:
Not exist Node
下載 .msi 安裝包 https://nodejs.org
安裝時(shí)(最好管理員運(yùn)行)勾選:Automatically install the necessary tools ...
等待 安裝程序 和 PowerShell 都執(zhí)行完成
Already exist Node
包括 nvm 管理的 node
refer to:
- https://github.com/nodejs/node-gyp
- https://github.com/Microsoft/nodejs-guidelines/blob/master/windows-environment.md#compiling-native-addon-modules
- https://github.com/nodejs/node-gyp/issues/762
- https://nodejs.org/api/addons.html
- http://www.itdecent.cn/p/af7f4bac2b0c
- https://blog.csdn.net/qq_33826977/article/details/78620363
setp
-
install Chocolatey (powershell or cmd)
// powershell (admin) Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) // cmd (admin) @"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin" -
install windows-build-tools by Chocolatey
// install: Visual C++ Build Tools // powershell/cmd (admin) choco install visualcpp-build-tools - options: A -
如果未安裝 python
- choco install python2
npm i -g node-gyp
npm config set msvs_version 2017 -g
-
node-gyp install
- powershell 需要修改執(zhí)行策略, 執(zhí)行:set-ExecutionPolicy RemoteSigned
-
node-gyp configure --msvs_version=2017
- 可忽略如下錯誤
gyp: binding.gyp not found (cwd: C:\Windows\system32) while trying to load binding.gyp gyp ERR! stack Error: `gyp` failed with exit code: 1 -
[可選]resolve error: if not defined npm_config_node_gyp
windows 中 2 選 1 即可
- npm config set node_gyp "C:\Users\usr\AppData\Roaming\nvm\v12.14.1\node_modules\node-gyp\bin\node-gyp.js" -g
- set npm_config_node_gyp=node C:\Users\usr\AppData\Roaming\npm\node_modules\node-gyp\bin\node-gyp.js -g
-
test: npm i -S
- mcrypt
- sqlite3
- node-sass
- kerberos