Ethers.js is a JavaScript library for interacting with the Ethereum blockchain.
It works in both frontend (React/Next.js) and backend (Node.js) environments.
You use it to:
- Connect wallets like MetaMask
- Read blockchain data (e.g., balances, block numbers)
- Interact with smart contracts (call/write functions)
- Send ETH and sign messages
- Listen to events on-chain
| Benefit | Description |
|---|---|
| β Lightweight | Much smaller than Web3.js |
| β Easy to Use | Clean, consistent API |
| β TypeScript support | Built-in typing support |
| β Secure | Handles checksum addresses, ENS, etc. |
| β Flexible | Works with all Ethereum-compatible chains (EVM) |
Used to connect to the blockchain (read-only)
const provider = new ethers.providers.Web3Provider(window.ethereum); // browser
const provider = new ethers.providers.JsonRpcProvider("<https://rpc-url>"); // backend
You can use this to:
await provider.getBalance("0x..."); // ETH balance
await provider.getBlockNumber(); // Latest block
Represents a wallet that can sign and send transactions (write)
const signer = provider.getSigner(); // userβs wallet (MetaMask)
const wallet = new ethers.Wallet(PRIVATE_KEY, provider); // backend wallet