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

React开发DApp时Web3实例化报错:https.Agent is not a constructor

React DApp中Web3通过HTTP/HTTPS RPC实例化时的构造函数错误问题

Uncaught TypeError: https.Agent is not a constructor

我使用React开发一款DApp,当尝试通过HTTPS或HTTP的RPC实例化Web3时出现上述报错,经排查确认错误源自web3-providers-http模块。使用MetaMask提供的window.ethereum作为提供者时,读写区块链交易均正常,现需解决该错误以正常完成交易。

复现步骤

var Web3 = require('web3');
var provider = 'https://mainnet.infura.io/v3/<PROJECT-ID>';
var web3Provider = new Web3.providers.HttpProvider(provider);
var web3 = new Web3(web3Provider);
web3.eth.getBlockNumber().then((result) => {
  console.log("Latest Ethereum Block is ",result);
});

版本信息

  • Web3.js Version 1.7.4
  • Vite Version 3.0.0

环境

  • 操作系统:macOs 11.5.2
  • 浏览器:Chrome、Firefox
  • Node.js版本:v12.22.0
  • NPM版本:7.7.6

内容的提问来源于stack exchange,提问作者Goutham J.M

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 05:45:33