Web3 Require is not defined



你好,我正在尝试将Web3链接到我的html。我在用这个https://github.com/ChainSafe/web3.js?source=post_page-----e3fbe89111c3-------------------------链接。

这是我在html文件中的代码

<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
<script src="app.js"></script>

这是我在app.js 中的代码

// In Node.js
const Web3 = require('web3');
let web3 = new Web3('ws://localhost:8546');
console.log(web3);

我一直试图在谷歌上搜索答案,但似乎找不到问题的答案。

// In Node.js
const Web3 = require('web3');

查看该注释。您在Node.js.中不是

您正在加载带有脚本元素的web3:

<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>

…所以不要试图在web浏览器中使用为Node.js设计的方法加载它。

(如果你想使用这种方法,那么你可以删除脚本元素,并使用Parcel或Webpack等工具将JS转换为浏览器友好的东西(。

最新更新