Windows 使用 node-gyp

  • 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

  1. 下載 .msi 安裝包 https://nodejs.org

  2. 安裝時(shí)(最好管理員運(yùn)行)勾選:Automatically install the necessary tools ...

  3. 等待 安裝程序 和 PowerShell 都執(zhí)行完成

Already exist Node

包括 nvm 管理的 node

refer to:

setp

  1. 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"
    
  2. install windows-build-tools by Chocolatey

    // install: Visual C++ Build Tools
    // powershell/cmd (admin)
    choco install visualcpp-build-tools
    
    -   options: A
    
  3. 如果未安裝 python

    • choco install python2
  4. npm i -g node-gyp

  5. npm config set msvs_version 2017 -g

  6. node-gyp install

    • powershell 需要修改執(zhí)行策略, 執(zhí)行:set-ExecutionPolicy RemoteSigned
  7. 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
    
  8. [可選]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
  9. test: npm i -S

    • mcrypt
    • sqlite3
    • node-sass
    • kerberos
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容