Development

Ethers.js vs Web3.js: Which Library Should You Use?

Comprehensive comparison of the two most popular Ethereum JavaScript libraries for Web3 development.

Mudaser Iqbal··8 min read

Library Overview

Ethers.js and Web3.js are the two most popular JavaScript libraries for interacting with Ethereum. Both enable connecting to Ethereum nodes, sending transactions, and interacting with smart contracts.

Ethers.js is newer, more modern, and has become the preferred choice for new projects. Web3.js is older with a larger existing codebase using it. Understanding both helps you work with any Ethereum project.

Key Differences

Size and Dependencies:
Ethers.js is smaller (116 KB) with fewer dependencies. Web3.js is larger (over 1 MB) with more dependencies. For frontend applications, smaller bundle size improves load times.

API Design:
Ethers.js has cleaner, more intuitive API with better TypeScript support. Web3.js API is more verbose and older in design.

Provider Management:
Ethers.js separates providers and signers cleanly. Web3.js combines them, which can be confusing.

BigNumber Handling:
Ethers.js uses its own BigNumber library. Web3.js uses BN.js. Both handle large numbers safely.

Documentation:
Ethers.js has excellent documentation with many examples. Web3.js documentation is improving but historically less comprehensive.

Community and Support:
Both have active communities. Ethers.js momentum is growing rapidly.

When to Use Each

Choose Ethers.js when:
Starting a new project
Bundle size matters (frontend applications)
You want modern, clean API
TypeScript support is important
Working with Hardhat (native integration)

Choose Web3.js when:
Working with existing codebase using Web3.js
Using Truffle framework (native integration)
Team is already familiar with Web3.js
Need specific Web3.js plugins or extensions

Many projects are migrating from Web3.js to Ethers.js for better developer experience and smaller bundle size.

Code Comparison

Connecting to Provider:
Ethers.js uses clean provider abstraction with JsonRpcProvider, InfuraProvider, etc. Web3.js uses single Web3 instance with provider parameter.

Sending Transactions:
Ethers.js separates signer from provider for better security. Web3.js combines them in accounts.

Contract Interaction:
Both support similar contract interaction patterns. Ethers.js has better TypeScript support with generated types.

Event Listening:
Ethers.js uses standard event emitter pattern. Web3.js has similar but slightly different API.

Utilities:
Both provide utilities for unit conversion, address validation, and encoding. Ethers.js utilities are generally more comprehensive.

The syntax differences are minor, but Ethers.js generally requires less boilerplate code.

Migration and Best Practices

Migrating from Web3.js to Ethers.js is straightforward:

Replace Web3 instance with ethers provider. Update contract instantiation to use ethers.Contract. Change transaction sending to use signer pattern. Update event listeners to ethers format.

Best practices for both:
Always validate addresses before using
Handle errors properly with try-catch
Use appropriate provider for your needs
Keep libraries updated for security patches
Test thoroughly on testnets first

For new projects, Ethers.js is recommended. For existing projects, evaluate migration benefits against effort required.

Both libraries are actively maintained and will continue to be supported. Choose based on your specific needs and constraints.

One Solidity tip + 1 case study per month

Ethers.js vs Web3.js: Which Library Should You Use? | Crypto Hawking