Introduction
web3.js is one of the original JavaScript libraries for interacting with the Ethereum blockchain. Created in 2015, it provides a comprehensive API for reading chain state, sending transactions, deploying contracts, and subscribing to events. It remains widely referenced in tutorials and production DApps.
What web3.js Does
- Connects to Ethereum nodes via HTTP, WebSocket, or IPC providers
- Encodes and decodes contract calls using ABI definitions
- Signs and sends transactions from local or injected wallets
- Subscribes to real-time events such as new blocks, pending transactions, and log filters
- Provides utility functions for unit conversion, hashing, and address checksum validation
Architecture Overview
web3.js v4 is a modular TypeScript rewrite organized into packages: web3-eth for core chain interaction, web3-eth-contract for smart contract abstraction, web3-eth-accounts for signing, web3-utils for helpers, and web3-net for network info. A central Web3 class ties them together and manages the provider connection. Plugins extend functionality without modifying the core.
Self-Hosting & Configuration
- Install with
npm install web3or import individual sub-packages for smaller bundles - Set the provider to a public RPC endpoint (Infura, Alchemy) or your own Geth/Reth node
- Configure gas price strategies using
web3.eth.calculateFeeData()for EIP-1559 transactions - Use
web3.eth.accounts.walletto manage local signing keys - Enable WebSocket provider for real-time subscriptions (
new Web3.providers.WebsocketProvider(url))
Key Features
- Plugin system allows community extensions without forking the core library
- First-class TypeScript support with full type inference on contract methods
- ENS (Ethereum Name Service) resolution built in
- Supports batch JSON-RPC requests for reducing network round trips
- Compatible with browser, Node.js, and React Native environments
Comparison with Similar Tools
- ethers.js — Lighter API with separate provider/signer pattern; web3.js uses a unified Web3 object
- Viem — Modern TypeScript-first alternative with tree-shaking; web3.js has broader legacy ecosystem support
- Wagmi — React hooks layer built on Viem; web3.js is framework-agnostic
- Alchemy SDK — Wraps ethers.js with managed services; web3.js is provider-independent
FAQ
Q: Is web3.js still maintained? A: The 4.x branch received active development through ChainSafe. The repository is now archived, but the library remains functional and widely used.
Q: Should I use web3.js or ethers.js for a new project? A: For new projects, ethers.js or Viem are generally recommended for their smaller bundle sizes and modern APIs. web3.js is a solid choice if your team already uses it or you rely on its plugin ecosystem.
Q: Does web3.js work with non-Ethereum chains? A: Yes. It works with any EVM-compatible chain (Polygon, BSC, Avalanche) by changing the provider URL.
Q: How do I handle contract events?
A: Use contract.events.EventName() with a WebSocket provider to receive real-time event logs.