使用 "npm i truffle -g" 安装 truffle 时出错

Error while installing truffle using "npm i truffle -g"

在我的桌面上安装 Truffle 时,出现匿名错误...

首先它显示与 python 相关的错误,这很奇怪,但后来我安装 python 现在这个错误

    npm ERR! code 1
    npm ERR! path C:\Users\Tanuj Sharma\AppData\Roaming\npm\node_modules\truffle\node_modules\ganache\node_modules\leveldown
    npm ERR! command failed
    npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node-gyp rebuild
    npm ERR! gyp info it worked if it ends with ok
    npm ERR! gyp info using node-gyp@8.3.0
    npm ERR! gyp info using node@16.13.2 | win32 | x64
    npm ERR! gyp info find Python using Python version 3.10.2 found at "C:\Python310\python.exe"
    npm ERR! gyp ERR! find VS
    npm ERR! gyp ERR! find VS msvs_version not set from command line or npm config
    npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
    npm ERR! gyp ERR! find VS checking VS2019 (16.11.31829.152) found at:
    npm ERR! gyp ERR! find VS "C:\Program Files (x86)\Microsoft Visual Studio19\BuildTools"
    npm ERR! gyp ERR! find VS - found "Visual Studio C++ core features"
    npm ERR! gyp ERR! find VS - found VC++ toolset: v142
    npm ERR! gyp ERR! find VS - missing any Windows SDK
    npm ERR! gyp ERR! find VS could not find a version of Visual Studio 2017 or newer to use
    npm ERR! gyp ERR! find VS looking for Visual Studio 2015
    npm ERR! gyp ERR! find VS - not found
    npm ERR! gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
    npm ERR! gyp ERR! find VS
    npm ERR! gyp ERR! find VS **************************************************************
    npm ERR! gyp ERR! find VS You need to install the latest version of Visual Studio
    npm ERR! gyp ERR! find VS including the "Desktop development with C++" workload.
    npm ERR! gyp ERR! find VS For more information consult the documentation at:
    npm ERR! gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
    npm ERR! gyp ERR! find VS **************************************************************
    npm ERR! gyp ERR! find VS
    npm ERR! gyp ERR! configure error
    npm ERR! gyp ERR! stack Error: Could not find any Visual Studio installation to use
    npm ERR! gyp ERR! stack     at VisualStudioFinder.fail (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:121:47)
    npm ERR! gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:74:16
    npm ERR! gyp ERR! stack     at VisualStudioFinder.findVisualStudio2013 (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:351:14)
    npm ERR! gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:70:14
    npm ERR! gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:372:16
    npm ERR! gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\util.js:54:7
    npm ERR! gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\util.js:33:16
    npm ERR! gyp ERR! stack     at ChildProcess.exithandler (node:child_process:404:5)
    npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:390:28)
    npm ERR! gyp ERR! stack     at maybeClose (node:internal/child_process:1064:16)
    npm ERR! gyp ERR! System Windows_NT 10.0.22000
    npm ERR! gyp ERR! command "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js" "rebuild"
    npm ERR! gyp ERR! cwd C:\Users\Tanuj Sharma\AppData\Roaming\npm\node_modules\truffle\node_modules\ganache\node_modules\leveldown
    npm ERR! gyp ERR! node -v v16.13.2
    npm ERR! gyp ERR! node-gyp -v v8.3.0
    npm ERR! gyp ERR! not ok
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     C:\Users\Tanuj Sharma\AppData\Local\npm-cache\_logs22-01-23T11_22_31_900Z-debug.log

我的vscode已经是最新版本

npm: '8.1.2', 节点:'16.13.2', v8: '9.4.146.24-node.14',

您必须安装 Visual Studio 社区 2022 以及 Visual Studio 代码。在安装 Visual Studio Community 时,您必须安装带有一些可选包的 C++ 桌面开发。

我找到了适合我的解决方案:

node-gyp 文档指出“node-gyp 要求您安装了 Python 的兼容版本,其中之一是:v3.6、v3.7、v3.8、或 v3.9。”,我有 3.10.2,正如我注意到的那样。

  • 运行npm install --global windows-build-tools。由于这会安装一些 2.7 Python 版本,我将其卸载以进行下一步。
  • 安装 Python 3.9 (https://www.python.org/downloads/release/python-3910/)
  • 安装node-gyp:npm install -g node-gyp
  • 清除 npm 缓存:npm cache clear --force
  • 终于打到了npm i -g truffle

顺便说一下,我 运行 Windows 10,节点 16.14.0 和 npm 8.3.1