Apple Silicon设备无法安装Hardhat Waffle问题求助
问题:安装Hardhat-Waffle依赖时node-gyp编译失败(Mac ARM64 + Node.js 18)
问题详情
我正在开发第一个DAPP,按照教程执行以下安装命令:
npm install --save-dev @nomiclabs/hardhat-waffle 'ethereum-waffle@^3.0.0' @nomiclabs/hardhat-ethers 'ethers@^5.0.0'
运行后出现编译报错,核心错误是bufferutil包的node-gyp构建失败,同时伴随多个依赖弃用警告。完整报错信息如下:
npm WARN deprecated ganache-core@2.13.2: ganache-core is now ganache; visit https://trfl.io/g7 for details npm WARN deprecated ganache-core@2.13.2: ganache-core is now ganache; visit https://trfl.io/g7 for details npm WARN deprecated testrpc@0.0.1: testrpc has been renamed to ganache-cli, please use this package from now on. npm WARN deprecated querystring@0.2.0: The querystring API is considered Legacy. new code should use the URLSearchParams API instead. npm WARN deprecated @ensdomains/ens@0.4.5: Please use @ensdomains/ens-contracts npm WARN deprecated @ensdomains/resolver@0.2.4: Please use @ensdomains/ens-contracts npm ERR! code 1 npm ERR! path /Users/phoenix_20/Desktop/Web3.0/smart_contract/node_modules/ganache-core/node_modules/bufferutil npm ERR! command failed npm ERR! command sh -c -- node-gyp-build npm ERR! gyp info it worked if it ends with ok npm ERR! gyp info using node-gyp@9.1.0 npm ERR! gyp info using node@18.4.0 | darwin | arm64 npm ERR! gyp info find Python using Python version 3.9.13 found at "/opt/homebrew/opt/python@3.9/bin/python3.9" npm ERR! gyp info spawn /opt/homebrew/opt/python@3.9/bin/python3.9 npm ERR! gyp info spawn args [ npm ERR! gyp info spawn args '/Users/phoenix_20/.nvm/versions/node/v18.4.0/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py', npm ERR! gyp info spawn args 'binding.gyp', npm ERR! gyp info spawn args '-f', npm ERR! gyp info spawn args 'make', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '/Users/phoenix_20/Desktop/Web3.0/smart_contract/node_modules/ganache-core/node_modules/bufferutil/build/config.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '/Users/phoenix_20/.nvm/versions/node/v18.4.0/lib/node_modules/npm/node_modules/node-gyp/addon.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '/Users/phoenix_20/Library/Caches/node-gyp/18.4.0/include/node/common.gypi', npm ERR! gyp info spawn args '-Dlibrary=shared_library', npm ERR! gyp info spawn args '-Dvisibility=default', npm ERR! gyp info spawn args '-Dnode_root_dir=/Users/phoenix_20/Library/Caches/node-gyp/18.4.0', npm ERR! gyp info spawn args '-Dnode_gyp_dir=/Users/phoenix_20/.nvm/versions/node/v18.4.0/lib/node_modules/npm/node_modules/node-gyp', npm ERR! gyp info spawn args '-Dnode_lib_file=/Users/phoenix_20/Library/Caches/node-gyp/18.4.0/<(target_arch)/node.lib', npm ERR! gyp info spawn args '-Dmodule_root_dir=/Users/phoenix_20/Desktop/Web3.0/smart_contract/node_modules/ganache-core/node_modules/bufferutil', npm ERR! gyp info spawn args '-Dnode_engine=v8', npm ERR! gyp info spawn args '--depth=.', npm ERR! gyp info spawn args '--no-parallel', npm ERR! gyp info spawn args '--generator-output', npm ERR! gyp info spawn args 'build', npm ERR! gyp info spawn args '-Goutput_dir=.'
解决方案
1. 配置ARM64兼容的编译环境
Mac ARM64架构下部分原生包编译需要额外配置,先确保安装了Xcode命令行工具:
xcode-select --install
然后临时设置环境变量,强制用x86_64工具链编译:
export npm_config_target_arch=x64 export npm_config_arch=x64 export npm_config_platform=darwin
2. 降级Node.js到LTS版本
Node.js 18对旧版原生包兼容性不佳,建议切换到16.x LTS版本(用nvm操作最方便):
# 安装16.x版本 nvm install 16 # 切换到该版本 nvm use 16
如果未安装nvm,可通过Homebrew安装:brew install nvm,安装后重启终端生效。
3. 使用官方集成包重新安装
原教程的命令依赖过时的ganache-core,改用Hardhat官方推荐的集成工具包,一次性解决所有依赖兼容问题:
# 删除旧的依赖文件 rm -rf node_modules package-lock.json # 安装官方工具包 npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox
@nomicfoundation/hardhat-toolbox包含了hardhat-waffle、ethers、chai等所有开发DAPP需要的工具,无需手动指定版本。
4. 验证安装结果
安装完成后,运行以下命令验证:
npx hardhat
如果弹出Hardhat的初始化菜单(创建项目、编译合约等选项),说明依赖安装成功。
内容的提问来源于stack exchange,提问作者MEHTA HARSH DHAVAL
相关产品推荐
相关产品推荐

