0

I have searched this and can't understand what is problem at my side. Please suggest. Thanks in advance.

index.html

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

index.js

const web3Obj = new web3(window.ethereum); console.log(web3Obj);

Error

index.js:1 Uncaught TypeError: web3 is not a constructor at index.js:1:17

Operating System

Ubuntu

2
  • 1
    Are you using const web3Obj = new web3(window.ethereum)? Pay attention to the lower case in "new web3", it should be "new Web3" (upper case W) Commented Mar 17, 2022 at 0:21
  • try this instead https://cdn.jsdelivr.net/npm/[email protected]/lib/index.min.js
    – Adam Boudj
    Commented Mar 18, 2022 at 16:10

1 Answer 1

1

There are a couple potential issues/solutions here:

  1. Your JS is loading out of order. If you are loading the web3 library after index.js, then index.js doesn't know what web3 is because it hasn't loaded yet. Remember that in the browser, JS files are loaded/executed in the order listed. You could also import or require the web3 object using the second listed JS file, rather than importing it via a <script> tag
  2. Metamask (or other wallet provider/web3 injector) is not installed in the browser you're using
  3. Your issue can probably be answered by these questions: How To Connect Web3 With MetaMask? or https://stackoverflow.com/questions/65802366/metamask-does-not-inject-window-ethereum-uncaught-in-promise-typeerror-canno
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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