Web3.js

On this page 7

Web3.js is an open-source JavaScript library that lets web pages, Node.js services and scripts communicate with Ethereum and other EVM-compatible blockchains through a node’s JSON-RPC interface. In the domain of Smart contracts within Crypto & blockchain, it was for years the default bridge between a dApp front end and deployed contracts, handling account access, transaction signing, contract calls and event subscriptions.

The library began in 2014 alongside the Ethereum client software and was later maintained by ChainSafe. Its version 4 rewrite in TypeScript modernized the codebase, yet in early 2025 the maintainers announced the project’s sunset, so new projects increasingly choose alternatives such as Ethers.js or viem.

How Web3.js talks to a blockchain node

Web3.js works as a client-side wrapper around JSON-RPC methods such as eth_call, eth_sendRawTransaction and eth_getLogs. The application creates a Web3 instance bound to a provider, an object that knows how to deliver requests to a node, and the library converts readable method calls into correctly encoded RPC messages and decodes the responses.

The provider determines who signs and where requests go. A browser wallet such as MetaMask injects an EIP-1193 provider, which keeps private keys inside the wallet and asks the user to approve each transaction, while a backend service typically uses an HTTP or WebSocket endpoint together with a locally held key.

The table below compares the provider types a developer can configure.

Provider typeTypical useMain constraint
Injected EIP-1193 providerBrowser dApps with a walletUser must approve every signature
HTTP providerBackend reads and scripted writesNo push events; polling required
WebSocket providerReal-time event subscriptionsConnections drop and need reconnection
IPC providerServer co-located with a nodeWorks only on the same machine
Provider types supported by Web3.js

Provider choice also shapes reliability. Public RPC endpoints apply rate limits and occasional inconsistencies between load-balanced nodes, so production services often run their own node or use several providers with fallback logic.

Core modules and what they do

Web3.js is organized into modular packages, so applications can import only the parts they need and reduce bundle size. Version 4 made this split explicit and added TypeScript type inference for contract methods generated from an ABI.

PackagePurposeExample operation
web3-ethChain queries and transactionsRead a balance or block
web3-eth-contractABI-based contract interactionCall a token transfer method
web3-eth-accountsLocal key management and signingSign a transaction offline
web3-eth-abiEncode and decode call dataDecode an event log
web3-utilsUnits, hashing and conversionsConvert Ether to wei
Main Web3.js packages

Interaction with a contract starts from its Application Binary Interface (ABI) and address. Read-only methods use call, which executes on the node without a transaction or gas fee, while state-changing methods use send, which builds a transaction, requests a signature and returns a receipt after the transaction is mined. Developers must still choose a sensible gas limit and fee parameters, or rely on the library’s estimation.

Web3.js compared with Ethers.js and viem

Web3.js, Ethers.js and viem solve the same problem with different design philosophies. The choice affects bundle size, type safety, maintenance outlook and how wallet and node responsibilities are separated.

LibraryDesign approachMaintenance outlook
Web3.jsLarge, object-oriented, batteries includedSunset announced in 2025
Ethers.jsCompact, strict provider and signer splitActively maintained
viemFunctional, tree-shakable, strongly typedActively maintained
Comparison of EVM JavaScript libraries

Ethers.js popularized the separation of provider and signer, which makes it harder to accidentally mix read access with key custody. Web3.js historically bundled these roles into one object, which is convenient but encourages keys to be loaded into the same context as application logic. The difference matters most in backend services that sign transactions automatically.

Working with Web3.js in practice

Typical Web3.js workflows follow a repeatable sequence: connect a provider, load the ABI, read state, prepare a transaction, sign it and wait for confirmation. Development environments such as Hardhat, Foundry and Remix IDE produce the ABI and run local test chains against which the same code can be exercised.

  1. Create a Web3 instance with the wallet or node provider.
  2. Instantiate a contract object from the ABI and deployed address.
  3. Use call to read state such as balances or allowances.
  4. Use send or sign locally, then broadcast the raw transaction.
  5. Wait for the receipt and check its status field and emitted events.

Numeric handling deserves care. Token amounts are integers in the smallest unit, and Web3.js version 4 returns BigInt values by default; code written for version 1, which returned strings or BN objects, often breaks silently during migration.

Pitfalls and security considerations

Most Web3.js incidents stem from application mistakes rather than library flaws. The library faithfully sends what the developer asks for, so errors in units, key handling or confirmation logic translate directly into lost funds or failed transactions.

MistakeConsequenceFix
Private key in front-end codeAnyone can drain the accountSign only through the user’s wallet
Ether and wei confusionAmounts off by a factor of 10^18Convert with utility functions
Treating send as finalLogic runs before confirmationWait for receipt and required confirmations
Ignoring reorganizationsEvents later disappearTrack block confirmations before acting
Unpinned dependency versionsSupply-chain compromise riskLock versions and audit updates
Common Web3.js mistakes and fixes

Because the project is no longer actively developed, teams maintaining existing Web3.js code should plan a migration path, freeze audited versions and monitor security advisories. The library remains readable and functional, but unpatched dependencies become a growing risk over time.

In everyday language

A blockchain node speaks a technical language that ordinary websites do not understand. Web3.js is a translator written in JavaScript: a website asks it to check a balance or run a contract function, and the library turns that request into the exact message the node expects.

It does not hold money and it is not a wallet. It simply carries requests between an app, a wallet and the blockchain, which is why one careless line of code can still send funds to the wrong place.

Seen in the real world

A community art collective runs a small website where members mint commemorative tokens from an existing ERC-721 contract. The front end uses Web3.js with the visitor’s injected browser wallet, so private keys never touch the site. When a member clicks the mint button, the page calls the contract’s price function with call, then sends the mint transaction with send, and the wallet shows a confirmation dialog.

The page waits for the receipt and two block confirmations before displaying the new token. After the maintainers announced the sunset, the collective pinned the library version and scheduled a gradual migration to viem, keeping the site working while reducing long-term maintenance risk.