Ethers.js

On this page 7

Ethers.js is an open-source JavaScript and TypeScript library for interacting with Ethereum and other EVM-compatible blockchains: it reads chain data, manages keys and signers, encodes calls through a contract’s ABI and sends signed transactions. In the work with smart contracts, it is the layer that connects a web application, script or backend service to deployed contract code.

The library is known for a small footprint, a clear separation between reading and signing, and extensive utilities for units, hashing and address handling. It is released under the MIT license and is widely used alongside development frameworks such as Hardhat.

How Ethers.js is structured

Ethers.js organizes blockchain access around a few core abstractions that keep responsibilities apart. A Provider offers read-only access to a node, a Signer holds or reaches a private key and authorizes transactions, and a Contract object turns ABI definitions into ordinary JavaScript methods.

This split is a deliberate security boundary. Code that only displays balances never needs a key, while code that sends value must obtain a signer explicitly, often from a browser wallet that asks the user to confirm each action. The table below lists the main classes and their roles.

Class or modulePurposeTypical use
ProviderRead-only connection to a nodeBalances, blocks, logs, gas prices
SignerAuthorizes messages and transactionsSending ether, calling state-changing functions
WalletSigner built from a private keyBackend scripts and test accounts
ContractTyped wrapper around a deployed contractCalling balanceOf or transfer
InterfaceABI encoding and decodingParsing event logs and revert data
UtilitiesUnits, hashing, addressesparseUnits, keccak256, getAddress
Core building blocks of the library

Providers, signers and contract calls in practice

Ethers.js supports several provider types that differ in transport and resilience. A JsonRpcProvider connects over HTTP to a node or hosted endpoint, a WebSocketProvider keeps a live channel for event subscriptions, a BrowserProvider wraps a wallet extension, and a FallbackProvider queries several backends and compares results.

Contract interaction follows two paths. Read calls to view or pure functions run locally on the node and cost no gas. Write calls produce a transaction that must be signed, broadcast and mined; the method returns a transaction response, and calling wait() returns the receipt with status and emitted events.

AspectRead callWrite call
Requires signerNoYes
Gas costNone for the callerPaid in the native token
ResultDecoded return valueTransaction response, then receipt
Changes stateNoYes, after inclusion in a block
Failure modeRevert error returned at onceRevert during estimation or execution
Read calls compared with write calls

Events are handled through filters and listeners. Applications can query historical logs with queryFilter or subscribe to new events, and the Interface class decodes the raw log topics into named arguments.

Version 5 and version 6 differences

The major release Ethers.js version 6, published in 2023, modernized the library and introduced breaking changes. Its most visible change is the switch from a custom BigNumber class to the native JavaScript bigint type, which removes a whole category of conversion errors but requires code updates.

AreaVersion 5Version 6
Large numbersBigNumber classNative bigint
Browser wallet providerWeb3ProviderBrowserProvider
Utility namespaceGrouped under ethers.utilsExported at top level
Language targetOlder JavaScript runtimesES2020 and later
Notable changes between major versions

Many tutorials and older projects still use version 5 syntax, so mixing examples across versions is a frequent source of confusion. Teams migrating should update numeric handling first, since arithmetic between bigint and ordinary numbers throws errors by design.

Ethers.js sits in a small family of EVM client libraries and works alongside, not instead of, development frameworks. Web3.js was the earliest widely used library, but its maintainers announced its sunset in 2025. Viem is a newer TypeScript-first library that emphasizes strict typing and modular imports.

ToolCategoryRelationship to Ethers.js
Web3.jsClient libraryOlder alternative, now sunset
ViemClient libraryModern alternative with strict types
HardhatDevelopment frameworkUses Ethers.js through a plugin in tests
FoundryDevelopment frameworkTests in Solidity; Ethers.js often used for front ends
OpenZeppelinContract librarySupplies contracts that Ethers.js calls
Position among Ethereum developer tools

Choosing among client libraries is mostly a question of team familiarity, typing needs and bundle size. Ethers.js remains a common default because of its stable API, broad documentation and built-in support for the Ethereum Name Service.

Common mistakes and security considerations

Most problems with Ethers.js come from unit handling, key exposure and incomplete error handling rather than from the library itself. Token amounts must always use integer base units, and private keys must never be shipped to a browser bundle.

  • Using floating-point numbers for token amounts instead of parseUnits with the correct decimals.
  • Embedding a private key in front-end code or a public repository.
  • Ignoring the receipt status, assuming a broadcast transaction has succeeded.
  • Connecting to the wrong chain ID, which leads to failed calls or replay confusion.
  • Trusting a single RPC endpoint for critical data without a fallback or confirmation depth.

Developers should also decode revert reasons and custom errors through the contract Interface, set reasonable confirmation counts for high-value operations, and review gas estimates before signing, since an unexpectedly high estimate often signals a failing or malicious call.

Understanding it simply

Ethers.js is a translator between a website and a blockchain. Blockchains speak a strict, technical language of encoded data and digital signatures, while web applications speak JavaScript. The library converts between the two.

When an app needs to show a balance, Ethers.js asks the network and returns a readable number. When the app needs to send a payment, Ethers.js prepares the request and passes it to the user’s wallet for approval. The app never needs to hold the secret key.

Applied in practice

A small team builds a donation page for a community project that accepts a dollar-pegged token on an Ethereum layer 2 network. The front end uses a BrowserProvider to connect to the visitor’s wallet and a read-only JsonRpcProvider to display the running donation total without asking anyone to connect first.

When a donor enters 25 tokens, the code converts the amount with parseUnits using 6 decimals, calls transfer on the token contract, and waits for one confirmation before showing a thank-you message with the transaction hash. A backend script, also written with Ethers.js, listens for Transfer events to the project address and records each donation in an accounting sheet. During testing, the team catches a bug where amounts were multiplied by 18 decimals instead of 6, which would have requested a trillion times the intended value; the wallet confirmation screen exposed the mistake before any funds moved.