无法访问JavaScript中的任何库

af7jpaap  于 2023-01-16  发布在  Java
关注(0)|答案(1)|浏览(144)

目前我正在做一个项目,我需要访问各种不同的库,但问题是我非常卡住了。我的程序是一个基于网络的应用程序,使用以太坊区块链(ganache,truffle,metamask)。例如,我试图通过一个JavaScript文件访问库web3-eth-accounts,我在我的网站上按下一个按钮就可以访问,但所有发生的是这样的:

Uncaught Error: Cannot find module 'web3-eth-accounts'
    at o (web3.min.js:1)
    at o (web3.min.js:1)
    at Object.init (Test.js:4)
    at HTMLButtonElement.onclick (statePrototype.html:26)

JavaScript语言:

Test = {

    init : function () {
        var Web3EthAccounts = require('web3-eth-accounts');

        var account = new Web3EthAccounts('ws://localhost:7545');

    }
}

包括web3-eth-accounts在内的所有库都在node-modules文件夹中。
Here is an image of all of the files

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Certificate System</title>
    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<!-- Pagrindinis htmlo kodas -->
<h1>Student State Prototype</h1>

<button onclick="App.render()">Show Student State</button>

<p id = "StatePrint"></p>

<button onclick="App.fail()">Make Student Fail</button>

<button onclick="App.complete()">Make Student Complete the Course</button>

<button onclick="Test.init()">Test</button>

<hr />

<form action="index.html">
    <button class="pageSwitch" type="submit">Back to HOMEPAGE</button>
</form>

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<script src="js/web3.min.js"></script>
<script src="js/truffle-contract.js"></script>
<script src="js/app.js"></script>
<script src = "js/Test.js"></script>
</body>
</html>
ajsxfq5m

ajsxfq5m1#

你所做的只是示例化它,你从来没有创建一个帐户。你必须调用

account.create();

从这里的文档:https://www.npmjs.com/package/web3-eth-accounts

相关问题