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
-
Answer is in this question here: ethereum.stackexchange.com/a/66396/9815– Travis ReederCommented Jan 30, 2019 at 16:44
3 Answers
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 ofwindow.web3
?– alperCommented Oct 25, 2018 at 15:21 -
-
@JesseBusman for the old lib try : github.com/ethereum/web3.js/tree/0.20.7/dist– Badr Bellaj ♦Commented Jun 19, 2019 at 11:31
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>
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 require
d 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..