12

I want to connect to the Ropsten test-net:

    // Is there is an injected web3 instance?
    if (typeof web3 !== 'undefined') {
      App.web3Provider = web3.currentProvider;
      web3 = new Web3(web3.currentProvider);
    } else {
      // If no injected web3 instance is detected, fallback to the TestRPC.
      App.web3Provider = new web3.providers.HttpProvider('http://localhost:8545');
      web3 = new Web3(App.web3Provider);
    }

I have http://localhost:8545 in this code. How do I connect to Ropsten instead of http://localhost:8545?

0

3 Answers 3

13

You can either run a local node and specify your chain using --chain ropsten when starting geth/parity and connect to localhost once sync is completed or you can connect to infura.io:

var web3 = new Web3(new Web3.providers.HttpProvider(
    'https://ropsten.infura.io/v3/[infura_project_id]'
));
3
  • geth --testnet --chain ropsten --rpc Got this error: Incorrect Usage. flag provided but not defined: -chain Commented Apr 18, 2018 at 17:01
  • 1
    @MuhammadShahzad try omitting --testnet
    – d9ngle
    Commented Apr 18, 2018 at 19:00
  • web3.eth.personal.currentProvider.connected = false for me. Tried Infura ropsten, kovan and local ganache. Commented Sep 7, 2020 at 0:18
3

You can also install metamask, which will inject web3 into the javascript runtime environment (assuming you are running this in the browser). From metamask, you can then select the chain/network as shown:

Metamask dropdown

1
  • For using this approach wee should run webpage on local server. Commented May 30, 2019 at 8:49
0

To get infura links, please use : https://infura.io/

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.