使用 npm install tr​​uffle -g 在 windows 10 上安装 Truffle 时出错

Getting Error on installing Truffle on windows 10 using npm install truffle -g

当我尝试安装 truffle 时,出现了这些错误:-

我之前已经安装了Node.js,我也安装了pip。

PS C:\Users\pk199\Documents> npm install -g truffle

npm ERR! code 1
npm ERR! path C:\Users\pk199\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.1 | win32 | x64
npm ERR! gyp info find Python using Python version 3.10.1 found at "C:\Users\pk199\AppData\Local\Programs\Python\Python310\python.exe"
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS msvs_version was set from command line or npm config
npm ERR! gyp ERR! find VS - looking for Visual Studio version 2022
npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
npm ERR! gyp ERR! find VS unknown version "undefined" found at "C:\Program Files\Microsoft Visual Studio22\Community"
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 valid versions for msvs_version:
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.19044
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\pk199\AppData\Roaming\npm\node_modules\truffle\node_modules\ganache\node_modules\leveldown
npm ERR! gyp ERR! node -v v16.13.1
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\pk199\AppData\Local\npm-cache\_logs22-01-24T15_16_27_276Z-debug.log

我也在 visual studio 2022 中安装了 C++ 桌面开发,但仍然出现此错误

以下是一些可能有帮助的参考资料:

尝试在管理员模式下通过 PowerShell 安装 Truffle(您处于管理员模式非常重要)

您需要允许脚本 运行 作为 PowerShell 中的管理员。为此,这里是 Stack Overflow 线程中的一些引用:

Enable Execution of PowerShell Scripts

PowerShell Scripts

Cannot Install Truffle

我 运行 PowerShell 中的命令 Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force 使它工作,但请在执行此操作之前参考上面的线程。

我可以在 Windows 10 上安装 truffle,方法是升级一个旧版本。 运行 PS 在管理员模式下(执行策略设置为“旁路”)。希望这有帮助。

npm install -g truffle@5.4.29

PS C:\Windows\system32> npm list -g
C:\Users\*****\AppData\Roaming\npm
+-- ganache-cli@6.12.2
+-- node-gyp@8.2.0
+-- npm@8.0.0
+-- **truffle@5.4.29**
`-- yarn@1.22.17


PS C:\Windows\system32> truffle
Truffle v5.4.29 - a development framework for Ethereum

Usage: truffle <command> [options]

Commands:
  build     Execute build pipeline (if configuration present)
  compile   Compile contract source files
  config    Set user-level configuration options
  console   Run a console with contract abstractions and commands available
  create    Helper to create new contracts, migrations and tests
  db        Database interface commands
  debug     Interactively debug any transaction on the blockchain
  deploy    (alias for migrate)
  develop   Open a console with a local development blockchain
  exec      Execute a JS module within this Truffle environment
  help      List all commands or provide information about a specific command
  init      Initialize new and empty Ethereum project
  install   Install a package from the Ethereum Package Registry
  migrate   Run migrations to deploy contracts
  networks  Show addresses for deployed contracts on each network
  obtain    Fetch and cache a specified compiler
  opcode    Print the compiled opcodes for a given contract
  preserve  Save data to decentralized storage platforms like IPFS and Filecoin
  publish   Publish a package to the Ethereum Package Registry
  run       Run a third-party command
  test      Run JavaScript and Solidity tests
  unbox     Download a Truffle Box, a pre-built Truffle project
  version   Show version number and exit
  watch     Watch filesystem for changes and rebuild the project automatically

See more at http://trufflesuite.com/docs

在安装 nodeJS 之前,您需要确保已经安装了依赖项,例如 python(我的版本是 3.10)。在尝试修复错误很多天后,我只是卸载了 nodeJs 并重新安装了它。 运行 npm install -g truffle 命令有效。

从错误日志中可以清楚地看出您缺少依赖项之一,即 Visual Studio

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 

连解决方案也在日志里

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

安装步骤Visual Studio

第 1 步:转到 https://visualstudio.microsoft.com/vs/ 并安装安装程序。

共有三个版本可供选择

  • 社区(免费)
  • 专业(付费)
  • 企业(付费)

第 2 步:运行 安装程序并选择 Desktop development with c++ 安装

安装 visual studio 后,重试 truffle 安装

注意:如果您正在使用 windows,那么您必须在安装 visual studio 后关闭终端,然后使用管理员权限再次打开终端并重试 truffle 安装。

对于windows尝试执行以下命令

npm install -g truffle@5.4.29