3

During the compilation of my JS code I used the statement Web3 = require('web3') for importing the web3 library. But since the require function doesn't work in a web application, I would like to know how it can be done.
Link

7

by web application you mean HTML/..

download web3js form github repo.

you could load it as normal js file using

<script src="web3js_path"></script>

e.g

    <script type="text/javascript" src="web3.min.js"></script>

   <script type="text/javascript">  if(typeof window.web3 !== "undefined" && typeof window.web3.currentProvider !== "undefined") {
        var web3 = new Web3(window.web3.currentProvider);
      } else {
        var web3 = new Web3();
      }
</script>
  • Should metamask be on on the browser because of window.web3? – alper Oct 25 '18 at 15:21
1

You can add it to your client code using package managers like bower:

bower install web3

or Component

component install ethereum/web3.js

or simply include distributed and minified library web3.min.js in your html file:

<script src="PATH/web3.min.js"></script>
0

The current answers detail how to include web3.js - a standalone Javascript file.

Your question implies that you are using a package manager - npm perhaps?

To pull in required packages, and utilise them within the browser you can utilise a tool like browserify or webpack.

Your question is however lacking in details so I am just making assumptions..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.