You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hardhat-Chai Matchers报错:isAddress不是函数问题求助

解决Hardhat测试中isAddress is not a function报错

问题原因

这是@nomicfoundation/hardhat-chai-matchers和Ethers.js版本不兼容导致的——该匹配器库内部调用Ethers的isAddress方法,但不同Ethers版本的API结构存在差异:Ethers v5里isAddress直接挂载在ethers对象上,要是你的依赖树里混进了更高版本(比如v6),或者匹配器版本和Ethers版本不对应,就会触发这个调用失败的错误。

修复方案

1. 锁定兼容的依赖版本

使用这套经过验证的稳定版本组合:

  • ethers: ^5.7.2
  • @nomicfoundation/hardhat-chai-matchers: ^1.0.6
  • hardhat: ^2.12.7

执行命令重装依赖:

npm uninstall ethers @nomicfoundation/hardhat-chai-matchers
npm install ethers@^5.7.2 @nomicfoundation/hardhat-chai-matchers@^1.0.6 --save-dev

2. 清理缓存与依赖文件

缓存或旧锁文件可能导致版本混乱,执行以下命令清理:

npx hardhat clean
rm -rf node_modules package-lock.json
npm install

3. 检查Hardhat配置文件

确保hardhat.config.js中正确导入了匹配器库:

require("@nomicfoundation/hardhat-chai-matchers");

4. 临时替代方案(若上述方法无效)

直接在测试中使用Ethers v5原生的地址验证方法,绕开匹配器库的问题:

const { ethers } = require("hardhat");
// 替换原依赖matchers的地址验证逻辑
expect(ethers.utils.isAddress(targetAddress)).to.be.true;

验证

重新运行测试套件:

npx hardhat test

内容的提问来源于stack exchange,提问作者Aung

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 05:22:44