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

编译Mantle众筹DApp时出现@nomiclabs/hardhat-ethers模块缺失错误

解决Hardhat编译时Cannot find module '@nomiclabs/hardhat-ethers'错误

问题描述

编译基于Mantle的众筹DApp时,执行yarn hardhat compile命令抛出模块缺失错误(所有相关文件已存在),完整报错信息如下:

PS C:\Users\MOON AI\Desktop\New folder\mantle_crowdfunding-dapp> yarn hardhat compile
yarn run v1.22.19
$ "C:\Users\MOON AI\Desktop\New folder\mantle_crowdfunding-dapp\node_modules\.bin\hardhat" compile
An unexpected error occurred:

Error: Cannot find module '@nomiclabs/hardhat-ethers'
Require stack:
- C:\Users\MOON AI\Desktop\New folder\mantle_crowdfunding-dapp\hardhat.config.js
- C:\Users\MOON AI\Desktop\New folder\mantle_crowdfunding-dapp\node_modules\hardhat\internal\core\config\config-loading.js
- C:\Users\MOON AI\Desktop\New folder\mantle_crowdfunding-dapp\node_modules\hardhat\internal\cli\cli.js
- C:\Users\MOON AI\Desktop\New folder\mantle_crowdfunding-dapp\node_modules\hardhat\internal\cli\bootstrap.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1048:15)
at Function.Module._load (node:internal/modules/cjs/loader:901:27)
at Module.require (node:internal/modules/cjs/loader:1115:19)
at require (node:internal/modules/helpers:119:18)
at Object.<anonymous> (C:\Users\MOON AI\Desktop\New folder\mantle_crowdfunding-dapp\hardhat.config.js:5:2)
...
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

解决方案

  • 安装缺失的依赖模块
    @nomiclabs/hardhat-ethers是Hardhat集成ethers.js的官方插件,需显式安装。执行命令:

    yarn add --dev @nomiclabs/hardhat-ethers ethers
    

    注:需同时安装ethers,因为该插件依赖ethers.js库。

  • 验证配置文件的导入语句
    打开hardhat.config.js,确认文件顶部已正确导入插件:

    require("@nomiclabs/hardhat-ethers");
    

    若项目使用ES模块(hardhat.config.mjs),则改为:

    import "@nomiclabs/hardhat-ethers";
    
  • 清理缓存并重新编译
    清理Hardhat编译缓存后重新执行编译:

    yarn hardhat clean
    yarn hardhat compile
    
  • 重置依赖安装(若上述步骤无效)
    删除项目目录下的node_modules文件夹和yarn.lock文件,重新安装所有依赖:

    # Windows系统执行
    rmdir /s node_modules
    del yarn.lock
    
    # macOS/Linux系统执行
    rm -rf node_modules yarn.lock
    
    # 重新安装依赖
    yarn install
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 02:32:31