簡體   English   中英

Windows 上的 node-gyp 5:找不到 npm\\node_modules\\node-gyp\\bin\\node-gyp.js

[英]node-gyp 5 on Windows: Cannot find npm\node_modules\node-gyp\bin\node-gyp.js

在 Windows 10 v1903 上運行 npm 6.11.3、node-gyp 5.0.3、VS2019,得到這個錯誤:

Cannot find module %AppData%\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js (其中%AppData%擴展為實際的用戶特定路徑)。

該錯誤僅在通過 npm 調用構建時發生,即:

npm run build

package.json文件scripts部分:

"scripts": {
  ...
  "build": "node-gyp build",
}

如果在 npm 上下文之外手動調用node-gyp build ,錯誤就會消失。

如何擺脫錯誤並仍然使用npm腳本進行構建?

弄清楚了。

在 Windows 10 構建系統上升級到 node-gyp 5 后,我開始收到臭名昭著的Error: Cannot find module ... npm\\node_modules\\node-gyp\\bin\\node-gyp.js ,由node_modules\\npm-lifecycle\\node-gyp-bin引起node_modules\\npm-lifecycle\\node-gyp-bin被 npm 推送到我的PATH之上,完全如這里所述。

我找到並嘗試過的現有補救措施都不能為我解決這個問題,包括安裝最新的windows-build-tools 這正是我嘗試過的,按以下順序:

  • 手動清理系統PATH和用戶PATH環境變量中的混亂和重復項
  • 重啟
  • choco unistall nodejs
  • rd /s %AppData%\\npm\\
  • rd /s %AppData%\\npm\\
  • choco install nodejs
  • npm install -g npm@latest
  • npm install -g node-gyp@latest
  • npm install -g typescript@latest

我仍然收到missing node-gyp.js錯誤。 有什么幫助是正確設置npm config set node_gyp以指向正確的node-gyp.js ,如建議here 然而不知何故,該線程中的腳本對我來說無法正常工作(以及我嘗試過的其他一些 PowerShell/Batch 腳本)。

最終,我想出了以下 PowerShell 6 one-liner 對我有用的方法

pwsh -Command npm config set node_gyp "`u{22}$(Join-Path $(npm root -g) -ChildPath 'node-gyp\bin\node-gyp.js')`u{22}"

希望這有助於其他人和未來的我。 我仍然建議通過上面的項目符號列表進行干凈的故障排除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM