1

The official documentation of web3.js has this little commented statement saying:

// in node.js use: const Web3 = require('web3');

(you can find it here: https://web3js.readthedocs.io/en/1.0/getting-started.html#adding-web3-js)

Well when I put that statement into my code, the web3 version that gets loaded is 0.19.0.

And when I take that statement out, the web3 version that gets loaded is 0.20.3

Either way, the version that I want - web3@1.0.0-beta.46 - is not getting loaded - and I can't for the life of me figure out why.

• I've already installed the latest version of Truffle and Web3 (web3@1.0.0-beta.46) a number of times, but every time I run my Dapp - on all my browsers, they're all telling me they're loading web3 0.2.x.

For example, here's a screen-grab of what Chrome is showing in the console when I run my DApp:

enter image description here




Here's what my code for this looks like:

initWeb3: function () {
    console.log("\n===================");
    console.log("===>in 'initWeb3'!!!");

    if (typeof web3 !== 'undefined') {
      // If a web3 instance is already provided by MetaMask:
      console.log("\n\n>FOUND a web3 provider! METAMASK!!!");
      App.web3Provider = web3.currentProvider;
      App.web3Object = new Web3(web3.currentProvider);
      console.log("1. web3Object.version = ", App.web3Object.version.api);

      var web3VersionFloated = parseFloat(App.web3Object.version.api);
      console.log("2. web3VersionFloated = ", web3VersionFloated);
      ...

The way I'm embedding web3 is through the <script> tags in my index.html page:

<head>
    <meta charset="UTF-8">
    ...

    <!-- 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>

</head>

I also tried this:

<script src="https://etherscan.io/jss/web3.min.js"></script>

But that didn't seem to make a difference either.

Of course the next step was to go through Terminal to see what's going on, and here's what it's saying when I run truffle version and npm ls web3:

enter image description here

Next, I looked inside the node_modules folder inside of my project's folder, and opened the package.json file. Here's what its got:

{
  "_from": "web3@^1.0.0-beta.46",
  "_id": "web3@1.0.0-beta.46",
  "_inBundle": false,
  "_integrity": "sha512-a+apA/pXiivrurVEq5Pv9+0GZ5vC6Utu8M9fojW9YUuvory29WFDg+tg+ITBmMvz/dQxInahNS32xJB6HhZbHg==",
  "_location": "/web3",
  "_phantomChildren": {},
  "_requested": {
    "type": "range",
    "registry": true,
    "raw": "web3@^1.0.0-beta.46",
    "name": "web3",
    "escapedName": "web3",
    "rawSpec": "^1.0.0-beta.46",
    "saveSpec": null,
    "fetchSpec": "^1.0.0-beta.46"
  },
  "_requiredBy": [
    "#USER",
    "/"
  ],
  "_resolved": "https://registry.npmjs.org/web3/-/web3-1.0.0-beta.46.tgz",
  "_shasum": "03b5eb25e4ca1130f4775e43964c19dad2aecdc2",
  "_spec": "web3@^1.0.0-beta.46",
  ...

Clearly, it says web3@^1.0.0-beta.46 everywhere.

I also checked the package.json file inside of my project's root folder, and its got this:

  ...

  "dependencies": {
    "dotenv": "^6.2.0",
    "truffle-hdwallet-provider": "^1.0.4",
    "web3": "^1.0.0-beta.46"
  }

In short, everywhere I've looked the indications are for web3 v. 1.0.xxx - so why are my browsers telling me they're loading web3 0.2.xxx?

Does this all go back to node.js somehow?

Or Is there some other file buried somewhere deep that is somehow overriding everything and injecting this older version?

Final note: the official web3.js docs say to use givenProvider:

const web3 = new Web3(Web3.givenProvider || "ws://localhost:8546");

but every single tutorial I've seen says to use currentProvider.

So which is it: givenProvider, or currentProvider?

I'd really appreciate any ideas that could help me solve this.

Your Answer

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

Browse other questions tagged or ask your own question.