[Ethereum] Uncaught ReferenceError: Web3 is not defined Solidity

errorjavascriptweb3-providersweb3js

I've been working with this project and I downloaded all dependencies and Web3 already but when I test my code it shows me this:

Uncaught ReferenceError: Web3 is not defined

in line 120.

This is my code:

<script>
if(typeof web3 !== 'undefined'){
    web3 = new Web3(web3.currentProvider); -this is where it errors
}else {
    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); 
}

web3.eth.defaultAccount = web3.eth.accounts[0];

var SuffrageContract = web3.eth.contract('example');
var Suffrage = SuffrageContract.at('example');
console.log(Suffrage);

I have already tried several methods like declaring

web3 = required('web3');

but it is still not working.

Best Answer

I hope you are using web3 as browser module.

As in their documentation, you can add the CDN link using <script> tags as below,

<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>