Ethers.js vs. Web3.js
As a developer building decentralized applications, one of the essential tools in your toolkit is a library for interacting with the Ethereum blockchain. Two of the most popular libraries for this purpose are Ethers.js and Web3.js.
While both libraries are robust and widely used, each has strengths and weaknesses.
This article will explore the similarities and differences between Ethers.js and Web3.js and why developers use them while building for the Ethereum ecosystem.
When are these libraries helpful?
Both ethers.js and web3.js are essential when interacting with the Ethereum blockchain programmatically. Developers use libraries for a variety of reasons. Still, one of the main reasons is to help them build software without rewriting code that other developers might have already written and tested.
While building dApps, both libraries allow developers to send transactions, query data, and interact with smart contracts in the backend. Developers can also use these libraries to integrate their dApps with popular wallets like MetaMask, which help improve the user experience, allowing users to interact with already familiar wallets. Another possible use case could be writing tests for smart contracts. A test suite covering your smart contract’s functionality helps improve security and its development process. Since smart contracts cannot be changed once deployed on the blockchain, it is crucial to ensure that the implemented methods perform precisely as intended and do not have any unintended functionalities.
Another common use for these libraries is for on-chain metrics. If you want more insights into the use and performance of your dApp, you can retrieve information with these libraries to analyze the data.
Overall, libraries like ethers.js or web3.js provide developers with a convenient way to interact with the blockchain and build decentralized applications while allowing for a better user experience.
Which one is more popular?
When deciding the tech stack for your application and choosing between libraries, one crucial consideration is the popularity of the libraries. A more widely used library will have a more extensive community behind it, more answers to problems you might face in the future, and probably better stability and reliability since they are used and tested more extensively.
By looking at the number of downloads and active users, we can get a sense of which library is more widely used in the developer community.
Before jumping into recent numbers, let’s analyze the past of these two libraries. The chart below shows the comparison in Google Trends of the search of “ethers js” vs. “web3 js”.
We can see from 2017 to 2019 that web3.js is the most searched term, with ethers.js barely coming up in any searches. During this period, web3.js was the most popular library, without a doubt. But, starting from January 2021, ethers.js began gaining popularity until now, when developers widely use both libraries.
According to npm trends, a website that tracks package downloads from npm, ethers.js has been gaining popularity steadily since 2018 and currently has over a million weekly downloads.
On the other hand, web3.js, which has been around since 2015, has seen a decline in downloads over recent years. Having about 600,000 weekly downloads currently. However, it’s important to note that web3.js is still a widely used library with a much larger overall install base. The chart below shows the npm downloads over time.
We also must consider that npm is one of many package managers out there, and other package managers have become much more popular among developers. Yarn and pNPM have gained much popularity in recent years. We should aggregate all package managers and see the final results to understand how much developers use these libraries more accurately. In yarn, ethers.js had 4.4 million downloads in the last 30 days, while web3.js had 2.4 million.
When considering forks, web3.js appears to have a considerable edge over ethers.js, with 4,500 and 1,500 forks, respectively.
These numbers may suggest that web3.js has a larger developer community and more active contributors, which may be an essential factor for some developers when making their choice.
Ultimately, the popularity of a library should not be the only factor considered when choosing between ethers.js and web3.js. Developers should also consider every project’s specific needs and requirements. However, these numbers provide valuable insights into both libraries’ current and past trends and usage in the blockchain development community.
As we have seen, web3.js has been around since the early days of blockchain development and has had extensive adoption in older projects. The Ethereum Foundation created the library and then delegated its maintenance to ChainSafe.
Reliable support and maintenance are critical components in ensuring the longevity and relevance of any technology ecosystem, and the Web3 community is no exception. With robust backing from reputable organizations, libraries like web3.js can deliver the reliability that developers depend on. These essential pillars of support are one of the reasons why we chose it as the foundation for Blast API. We are confident that Blast API can meet the demands of Web3 developers and continue to evolve with the industry.
Updates are released regularly, fixing bugs and adding new features. There were some minor breaking changes in the last release of web3.js version 4.x. While using any library, use the latest version; you can review the latest 4.x breaking changes here.
The unpacked size of web3.js is around 1.1 MB which can cause lower performance or seem a little heavy when used in smaller dApps. This library contains four modules: web3-eth, web3-shh for the whisper protocol, web3-bzz for the swarm protocol, and web3-utils, which include helper functions for dApp developers.
One last thing to mention is that the license for web3.js, which is free software, can be redistributed and modified under the terms of the GNU Lesser General Public License.
Overall, web3.js has the Ethereum Foundation’s support, has been around since the beginning, is updated regularly, and has a big community behind it. With all the blogs and tutorials around, it’s easy to start your blockchain developer journey with web3.js. Also, many influential and foundational Web3 projects were built using this library.
Unlike web3.js, ethers.js is a relatively new library, released in 2016 but gaining more popularity in 2021. Over the last few years, it has gained popularity mainly in new projects and developers wanting to learn about blockchain development. Richard Moore, a software developer from Canada specializing in cryptography and blockchain technologies, created and maintains ethers.js. Richard gets funding to develop ethers from various places, such as Gitcoin Grants, GitHub sponsors, and the Ethereum Foundation.
One of the standout features of ethers.js is its strong typing, which provides developers with enhanced safety and a more intuitive API. The library was built with TypeScript from the ground up, meaning developers can write more robust code and benefit from autocomplete functionality in their IDEs, for example. Another cool feature is that you can use ENS domains as addresses since they are considered first-class citizens.
Additionally, the library is modular, meaning developers can choose which components to use and only load those into their dApps, improving performance. The size of ethers.js is much smaller than that of web3.js, coming in at around 350kb when unpacked. A smaller bundle size makes it a more attractive option for smaller dApps, as it won’t impact performance as much as larger libraries. Ethers.js consists of several modules, including ethers-eth for Ethereum-specific functionality, ethers-contracts for working with smart contracts, and ethers-utils for general-purpose utility functions.
Similar to web3.js, ethers.js also undergoes regular updates and bug fixes. Nevertheless, it is worth mentioning that the frequency of updates for ethers.js is relatively lower than that of web3.js. The latest version of ethers.js is v6. The main focus of this release was extensibility and encapsulation while improving performance to be much faster than its predecessor. You can read more about version 6 here.
Ethers.js uses the MIT license, an open-source license that allows for more flexibility than the GNU Lesser General Public License used by web3.js.
In summary, ethers.js has strong typing and a modular design, making it a popular choice for many Ethereum developers. While updates are released less frequently than web3.js, the library is maintained regularly and receives bug fixes. The smaller size of the library also makes it a more performant option for smaller dApps, but if you are looking to work with more foundational and older projects, they will probably be using web3.js.
A new player in the ecosystem
Of course, a new library comes with all the risks and drawbacks, such as small community size, low compatibility, and more. The creators of wagmi, a collection of React hooks for Ethereum, developed Viem. It is not yet known if Viem will be more broadly used amongst blockchain developers to create dApps, tooling, and much more, but it is important to keep an eye on its development.
Both ethers.js and web3.js libraries have been around for quite a long time and have shown their robustness and resilience. Even if the complexity of these libraries differ, they are constantly evolving to a point where similar results can be achieved by using either one of them. Depending on the project you are building or if you are trying to learn, you’ll find different libraries helpful. It is always good, as developers, to keep researching and supporting the libraries that simplify our coding journey and help us build unique products with less effort each time.