Skip to content

Basic Required Setup

TXTCLASS edited this page Oct 12, 2017 · 3 revisions

For each script you run you should have the following at the top. Keep in mind this is how you would set up your script if you are using Node.js. For more information about other setups, read the official documentation.

Require the Web3 Module

var Web3 = require('web3');

After you install the module via npm you'll have a folder called "web3" in your apps "node_modules" folder. In there is all the good stuff Web3 requires to run and be generally happy with its meager existence. You'll need to require it each time you start up a new script.

Setting a Provider

Of course Web3 is useless without an Ethereum node to talk to. If you wish to run your own node you would use something like this:

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

You would have to tell Geth the rpc port to use as well as the ip address. However to reduce effort and make this easy for us noobs, all the scripts in this repo use Infura as our Ethereum provider, which looks like this:

web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/YOUR-API-TOKEN-HERE'));

The reasoning is simply its easier than using our own node. Some Web3 functions can't work without a full node, notably eth.getTransaction which if you tried to run using a light client (spv) you'd get a null response. So for ease of use and simplicity, Infura is the way to go.

Now this setup works fine for just playing around, if you intend on building an full app I'd suggest you learn how to maintain a node yourself as Infura does have limitations on calls and depending on how active your app is you may get cut off if you exceed that limit.

That's more or less the required basic setup you need to started with Web3 and Node.js! Once your Web3 folder is required and your provider is set, you're ready to go.