Web3: No provider set error

0 votes

Here's a part of the code:

const Web3 = require('web3');

let web3 = new Web3('http://localhost:8545');

let MyContract = new web3.eth.Contract(abi);

let accounts = await web3.eth.getAccounts();
let contract = await MyContract.deploy({data: code})
  .send({from: accounts[0], gas: 1000000});

contract.events.MyEvent((error, result) => {
  if (error) {
    return console.error(error);
  }
}

When I run the code, I get the following error:

Error: No provider set.
    at Subscription.subscribe (/node_modules/web3-core-subscriptions/src/subscription.js:199:20)
    at Contract._on (/node_modules/web3-eth-contract/src/index.js:634:18)

How can I solve this?

Oct 22, 2018 in Blockchain by digger
• 26,550 points
75 views
I have the same issue. Did you manage to fix this?
Yes. I followed the solution mentioned here: https://www.edureka.co/community/28159/web3-no-provider-set-error?show=28160c#28160

It worked for me

1 answer to this question.

0 votes

To solve this error, you need to pass in an HttpProvider when creating Web3. Change

let web3 = new Web3('http://localhost:8545');

to

let web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));

or, set the provider after creating Web3:

const web3 = new Web3();

web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));

This should work

answered Oct 22, 2018 by Omkar
• 67,660 points

Related Questions In Blockchain

0 votes
1 answer

"set RNG(RandomUtils.Random) before generating random numbers" error

Use the following code, you just have ...READ MORE

answered Jul 5, 2018 in Blockchain by slayer
• 29,170 points
59 views
0 votes
1 answer

How to set web3 solidity variable value?

The setLog transaction has not yet been mined by ...READ MORE

answered Jul 6, 2018 in Blockchain by Christine
• 15,790 points
155 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
169 views
0 votes
1 answer
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,620 points
156 views
0 votes
1 answer

Hyperledger Fabric: "No go in PATH" error.

The error is because the go path ...READ MORE

answered Jun 27, 2018 in Blockchain by Omkar
• 67,660 points
351 views
0 votes
2 answers

Truffle migrate gives “ Error: No network specified. ”

You have to mention on which network ...READ MORE

answered Aug 13, 2018 in Blockchain by Omkar
• 67,660 points
226 views