4 min read
Overview
When someone thinks of developing a dApp the first tool that comes to their mind is web3.js which is pretty common because of its popularity in the community and wide use cases, dApp development has been consistently growing and there are a lot of developers who want to learn dApp development. The web3.js library is sometimes not suitable because of it’s heavy package size. So here the ethers.js library comes into the picture, ethers.js is an alternative to web3.js library and it is very compact as compared to web3.js. It is only 88kb when compressed, ethers.js is open source and very well tested, it is maintained by dedicated contributors.
ethers.js is well suited for those who are relatively new to the dApp/blockchain development as it is very easy to use. Lots of new companies and projects are preferring ethers.js over other libraries like web3.js because of its performance profile with regards to the browser. Typescript has been gaining a lot of popularity lately, so if you’re amongst the typescript aficionados - don’t worry makers of ethers.js saw the future and made it future proof with will full Typescript source.
In this article, we'll cover installing Ethers.js, use it to connect to a web3 provider, and get the latest block height.
Prerequisites
- Node.js and Ethers.js (version 6) installed on your system
- A text editor
- Terminal aka Command Line
Installing Ethers.js library
First, let's make sure we have NodeJS installed. Pretty simple, open a terminal and run:
$ node -v
if not installed, you can download the LTS version of NodeJS from the official website.
If you want to use Ethers.js in the browser simply add the following script tag in your HTML file:
<script type="module">
import { ethers } from "https://cdnjs.cloudflare.com/ajax/libs/ethers/5.7.2/ethers.min.js";
// Your code here...
</script>
OR
Now let's install the ethers.js package, which will help us to connect to the ethereum network and build awesome dApp frontends. You can install it with npm from the command line:
$ npm install --save ethers
The most common issue at this step is an internal failure with `node-gyp`. You can follow node-gyp installation instructions here.
Note: You will need to have your python version match one of the compatible versions listed in the instructions above if you encounter the node-gyp issue.
Another common issue is a stale cache, clear your npm cache by simply typing the below into your terminal:
$ npm cache clean
If everything goes right and ethers.js get installed correctly and you’re halfway there!