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

使用Hardhat编译智能合约时遇模块找不到错误求助

Hardhat编译智能合约时ERR_DLOPEN_FAILED错误解决方法

问题详情

编译智能合约时触发模块加载失败错误,具体为系统找不到@nomicfoundation/solidity-analyzer-win32-x64-msvc对应的二进制文件,错误码ERR_DLOPEN_FAILED。

错误日志

Error: The specified module could not be found.
\\?\C:\Users\rehan\Downloads\hardhat-smartcontract-auction\node_modules\@nomicfoundation\solidity-analyzer-win32-x64-msvc\solidity-analyzer.win32-x64-msvc.node
    at Object.Module._extensions..node (node:internal/modules/cjs/loader:1210:18)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Module.require (node:internal/modules/cjs/loader:1028:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:\Users\rehan\Downloads\hardhat-smartcontract-auction\node_modules\@nomicfoundation\solidity-analyzer\index.js:69:29)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12) {
  code: 'ERR_DLOPEN_FAILED'
}

项目package.json配置

{
  "devDependencies": {
    "@chainlink/contracts": "^0.4.1",
    "@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers@^0.3.0-beta.13",
    "@nomiclabs/hardhat-etherscan": "^3.1.0",
    "@nomiclabs/hardhat-waffle": "^2.0.3",
    "chai": "^4.3.6",
    "dotenv": "^16.0.2",
    "ethereum-waffle": "^3.4.4",
    "ethers": "^5.7.0",
    "hardhat": "^2.11.0",
    "hardhat-contract-sizer": "^2.6.1",
    "hardhat-deploy": "^0.11.13",
    "hardhat-gas-reporter": "^1.0.8",
    "prettier": "^2.7.1",
    "prettier-plugin-solidity": "^1.0.0-beta.24",
    "solhint": "^3.3.7",
    "solidity-coverage": "^0.7.21"
  },
  "dependencies": {
    "@nomicfoundation/solidity-analyzer-win32-x64-msvc": "^0.0.3"
  }
}

已尝试操作

  • 创建新Hardhat项目并迁移合约,问题复现
  • 必须使用npm install @nomicfoundation/solidity-analyzer-win32-x64-msvc --force才能完成安装,普通安装失败,安装输出如下:
npm WARN using --force Recommended protections disabled.
npm WARN ERESOLVE overriding peer dependency
npm WARN While resolving: @nomiclabs/hardhat-waffle@2.0.3
npm WARN Found: @nomiclabs/hardhat-ethers@0.3.0-beta.13
npm WARN node_modules/@nomiclabs/hardhat-ethers
npm WARN   dev @nomiclabs/hardhat-ethers@"npm:hardhat-deploy-ethers@^0.3.0-beta.13" from the root project
npm WARN
npm WARN Could not resolve dependency:
npm WARN peer @nomiclabs/hardhat-ethers@"^2.0.0" from @nomiclabs/hardhat-waffle@2.0.3
npm WARN node_modules/@nomiclabs/hardhat-waffle
npm WARN   dev @nomiclabs/hardhat-waffle@"^2.0.3" from the root project
npm WARN
npm WARN Conflicting peer dependency: @nomiclabs/hardhat-ethers@2.1.1
npm WARN node_modules/@nomiclabs/hardhat-ethers
npm WARN   peer @nomiclabs/hardhat-ethers@"^2.0.0" from @nomiclabs/hardhat-waffle@2.0.3
npm WARN   node_modules/@nomiclabs/hardhat-waffle
npm WARN     dev @nomiclabs/hardhat-waffle@"^2.0.3" from the root project

up to date, audited 2010 packages in 43s

162 packages are looking for funding
  run `npm fund` for details

54 vulnerabilities (14 moderate, 37 high, 3 critical)

To address issues that do not require attention, run:
  npm audit fix

To address all issues possible (including breaking changes), run:
  npm audit fix --force

Some issues need review, and may require choosing
a different dependency.

Run `npm audit` for details.

解决方案

1. 移除手动安装的平台专属分析器包

@nomicfoundation/solidity-analyzer-win32-x64-msvc是平台专属二进制包,Hardhat会自动根据系统环境拉取对应版本,无需手动安装。先卸载它:

npm uninstall @nomicfoundation/solidity-analyzer-win32-x64-msvc

2. 清理缓存并重装依赖

缓存损坏可能导致模块加载异常,执行以下步骤:

  • 删除本地依赖文件和锁文件:
    Windows系统:
    rmdir /s /q node_modules && del package-lock.json
    
    类Unix系统:
    rm -rf node_modules package-lock.json
    
  • 清理npm缓存:
    npm cache clean --force
    
  • 重新安装所有依赖:
    npm install
    

3. 解决Hardhat Waffle依赖冲突

从安装日志可见,@nomiclabs/hardhat-waffle@2.0.3要求@nomiclabs/hardhat-ethers@^2.0.0,但你使用的hardhat-deploy-ethers(别名@nomiclabs/hardhat-ethers)版本为0.3.0,存在版本冲突。替换为兼容包:

  • 卸载冲突依赖:
    npm uninstall @nomiclabs/hardhat-ethers @nomiclabs/hardhat-waffle
    
  • 安装适配hardhat-deploy的waffle包:
    npm install --save-dev hardhat-deploy-waffle
    
  • 更新package.json的devDependencies,确保包含:
    "hardhat-deploy-waffle": "^0.1.10"
    

4. 降级Hardhat版本(可选)

若上述方法无效,尝试降级到更稳定的Hardhat版本:

npm install --save-dev hardhat@2.10.0

5. 检查系统依赖(Windows专属)

Windows下.node模块加载失败可能缺少Visual C运行库,安装微软官方的Visual C Redistributable for Visual Studio 2015-2022,重启终端后再尝试编译。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 10:24:47