I am creating a DApp where I want to show the public variable's information from the contract. I want my users who don't have metamask to see this information also. Is there a way I can do that?

If I use the DApp without using metamask then it returns that 'web3' is not defined.

Here is my code snippet of app.js when page loads [throws error at line 3]:

$(document).ready(function() {
// Ropsten:
var contractAddress = "0x208122Dc624EF89fD1960B060775439494FAa9ab";

var version = web3.version; // throws error at this line.
// Modern dapp browsers...
if (window.ethereum) {
    window.web3 = new Web3(ethereum);
    try {
        // Request account access if needed
    } catch (error) {
        // User denied account access...
// Legacy dapp browsers...
else if (window.web3) {
    window.web3 = new Web3(web3.currentProvider);
// Non-dapp browsers...
else {
    console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');

window.ethereum.on('accountsChanged', function(accounts) {


Contract = new web3.eth.Contract(abi, contractAddress);


Uncaught ReferenceError: web3 is not defined at HTMLDocument. (app.js:6) at i (jquery-2.2.4.min.js:2) at Object.fireWith [as resolveWith] (jquery-2.2.4.min.js:2) at Function.ready (jquery-2.2.4.min.js:2) at HTMLDocument.J (jquery-2.2.4.min.js:2)

  • throws error at line 3 - what is line 3 and what is the error? Commented Dec 30, 2019 at 10:36
  • I have updated the question with the error and the error line Commented Dec 30, 2019 at 11:32
  • Please specify which line of code (i.e., post the actual code, and no images please, just plain text). Commented Dec 30, 2019 at 11:44
  • it is throwing error that "web3 is not defined" and line 3 i.e. (var version = web3.version;) please check the code snippet as well. Commented Dec 30, 2019 at 11:45
  • 1
    With regards to the line var version = web3.version - it is not clear why you are calling this line before instantiating the web3 object. It is also not clear what role window.web3 (in opposed to web3) has in your code. Finally, though not directly related, it is not clear why you reinitialize the window.web3 object if it exists (else if (window.web3) window.web3 = ...)! Commented Dec 30, 2019 at 12:00

1 Answer 1


In the case you like to use web3 without Metamask you have to include web3.js for the browser and connect with a provider (here ganache on localhost):

    <!DOCTYPE html>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
        <title>Using web3 API with MetaMask</title>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/web3.min.js"></script>
            window.addEventListener('load', function () {
                if (!window.ethereum) {
                    window.web3 = new Web3(new Web3.providers.HttpProvider(''));
                    window.web3.eth.getAccounts(function (err, acc) {
                        if (err != null) {
                            self.setStatus("There was an error fetching your accounts");
                        if (acc.length > 0) {

Expected console output of browser is:

enter image description here

  • Are there any free providers that don't require registration?
    – Qwerty
    Commented Aug 27, 2022 at 21:59

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.