Introduction
ethers.js is a widely used JavaScript library for Ethereum development created by Richard Moore. Its design separates the provider (read-only connection) from the signer (wallet with private key), making code simpler and more secure. ethers.js is known for its small footprint, thorough documentation, and TypeScript-first approach.
What ethers.js Does
- Connects to Ethereum nodes through JSON-RPC, WebSocket, Infura, Alchemy, and Etherscan providers
- Deploys and interacts with smart contracts via typed Contract objects
- Manages wallets, signs transactions, and supports HD derivation paths
- Encodes and decodes ABI data, function selectors, and event logs
- Resolves ENS names and supports EIP-712 typed data signing
Architecture Overview
The library is structured around four core concepts: Provider (read-only chain access), Signer (write access with a private key or hardware wallet), Contract (typed interface to deployed contracts), and Utils (encoding, hashing, formatting). In v6, the library is split into sub-packages for tree-shaking. The provider layer abstracts multiple backend transports behind a unified interface, while Contract objects automatically generate callable methods from ABI definitions.
Self-Hosting & Configuration
- Install with
npm install ethersfor the full bundle or import sub-paths for smaller builds - Create a provider by passing an RPC URL to
JsonRpcProvideror useBrowserProviderfor MetaMask - Instantiate wallets with
new ethers.Wallet(privateKey, provider)for signing - Use
Contractfactory with address + ABI + signer/provider to interact with deployed contracts - Configure custom networks by passing chain ID and RPC details to the provider constructor
Key Features
- Small bundle size compared to alternatives, with tree-shakeable sub-packages in v6
- Provider/Signer separation enforces security best practices
- Human-readable ABI format simplifies contract interface definitions
- Built-in ENS support for resolving
.ethnames to addresses - Extensive TypeScript typings with inferred contract method signatures
Comparison with Similar Tools
- web3.js — Larger API surface with plugin system; ethers.js is leaner with clearer abstractions
- Viem — Newer TypeScript library with composable actions; ethers.js has a larger existing user base
- Wagmi — React hooks built on Viem; ethers.js is framework-agnostic and lower level
- Alchemy SDK — Wraps ethers.js with Alchemy-specific features; ethers.js is provider-neutral
FAQ
Q: Should I use ethers.js v5 or v6? A: v6 is the current release with improved TypeScript support and smaller bundles. v5 is still widely used but will receive only critical fixes.
Q: How do I connect to MetaMask in a browser?
A: Use new ethers.BrowserProvider(window.ethereum) to wrap the injected provider and request account access.
Q: Can ethers.js work with Layer 2 networks? A: Yes. Point the provider to any EVM-compatible RPC endpoint (Arbitrum, Optimism, Base, Polygon).
Q: Is ethers.js suitable for backend use? A: Yes. It works in Node.js for indexers, bots, and backend services alongside browser DApps.