How to use the ethers.js library to connect to the Ethereum Network

How to use the ethers.js library to connect to the Ethereum Network

If you need to connect to Ethereum Network easily, then the best way for you is to use ethers.js. When we talk about dApp development, users usually choose to work with Web3.js, although it is not always appropriate due to the enormous size of the package. The alternative library ethers.js can assist you because it is more compact. Here you’ll learn how to connect to the Ethereum network using ethers.js.

What is ethers.js?

Similar to web3.js, the ethers.js library offers a collection of tools for interfacing with Ethereum nodes using JavaScript. A dApp’s user interfaces and external code can be created in any language and installed on any server to communicate with internal logic. Smart contracts, frontend logic, and data storage are the three main categories that make up a dApp.

Ethers.js and web3.js are two free JavaScript libraries that let programmers communicate with the Ethereum blockchain and carry out various functions. This library is excellent for people who lack sufficient knowledge of dApp development. Many businesses and projects prefer to use Ethers.js due to its simplicity of use. It features well-tested Typescript source code and is only 88 kilobytes in size when compressed.

How to install Ethers.js?

NodeJS must be installed in order to install Ethers.js. Open a terminal and type the command:

$ node -v

If the installation is unsuccessful, you may download NodeJS LTS from the official website.

You must execute the following commands in your HTML file to use the library in your browser:

<script charset=”utf-8″




To use an Ethereum node and install the dApp interfaces that are needed, you should run the following command:

$ npm install –save ethers

At this time, a node-gyp internal failure could happen. You should set up node-gyp to resolve this issue.

The installation of node-gyp requires Python and C++.

There may also be a problem with an outdated cache when installing the library. To solve it, write the following command:

$ npm cache clean

Let’s move on to how to use an Ethereum node.

Operate on the Ethereum node.

On the Ethereum Network, you’ll be able to build your projects using any client. Geth or OpenEthereum (fka Parity), for instance, will work. There are easier solutions, but they are extremely difficult to utilize. Take NOWNodes’s free Ethereum endpoint as an illustration. Copy the endpoint after creating your API key to access the Ethereum node:

NOWNodes Ethereum endpoints

Make sure to copy and preserve this because you’ll need it in the following steps.

How to connect using Ethers?

The next thing you need to do is write a brief script in your favorite terminal that will ask the node for the block height. Make it index.js. the following commands:

var ethers = require(‘ethers’);  


var customHttpProvider = new ethers.providers.JsonRpcProvider(url);

customHttpProvider.getBlockNumber().then((result) => {

    console.log(“Current block number: ” + result);


Then you’ll have to replace the command ‘ADD_YOUR_ETHEREUM_NODE_URL’ with the ISP link you copied before.

In this Ethereum development step, the first command you import is the ethers library you installed. Then you specify the URL of the NOWNode’s node, and in the third line, you create an instance of the ethers JsonRpcProvider instance. Lines 4-6 of the following command are used to write the successful response from the server to the terminal after calling getBlockNumber for the ethers json rpc object.

You should save this bit of code in your index.js file.

Time to start.

Now it remains to run the script you created using the node command. After that you’ll see the last Ethereum blockchain:

$ node index.js

 It should look like this:

If you have done everything correctly, you should have successfully connected to the Ethereum network and can start your Ethereum app development.

A little bonus.

If you need to use the ethers.js library with truffle, then you’ll be able to just use the package from the same developer to make it easier on yourself. This package replaces most of the truffle and contains TestRPC included with ethers hook-ups. 

You must add this command to the code to start the provider for all wallet instances using the ethers library:

const provider = ethers.getDefaultProvider

You can also get more information about ethers here. If you have to use a front-end application, then webpack is the right one for you, with which you can achieve the results you need.