web3-utils is a core package within the web3.js ecosystem, providing a collection of essential utility functions for Ethereum dApp development. These utilities cover common tasks such as converting between different Ether units (e.g., Ether to Wei), validating Ethereum addresses, cryptographic hashing (like SHA3), and handling large numbers. As of the provided information, the package version is 4.3.3, though recent releases indicate active development up to v4.16.0 within the broader web3.js monorepo. It maintains a frequent release cadence, often aligning with fixes and features across the web3.js suite. A key differentiator is its tight integration and API consistency with the rest of the web3.js library, making it the de-facto choice for projects built on web3.js. It ships with full TypeScript support, ensuring type safety for modern JavaScript projects.
npm install web3-utilsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates common web3-utils functions: converting between Ether and Wei, validating an Ethereum address, calculating a SHA3 hash, and using `toBigInt` for large number handling.
Migrate all code that manipulates large numbers to use native `BigInt` types. Replace `toBN()` with `toBigInt()`, and adapt arithmetic operations (e.g., `+`, `-`, `*`, `/`) to work with `BigInt` (e.g., `10n + 5n`). Ensure all number-like inputs are explicitly converted to `BigInt` where expected or passed as strings.
Always install all `web3.js` related packages from the same major version to ensure compatibility. For example, if you are using `web3@4.x.x`, ensure all sub-packages like `web3-utils`, `web3-eth`, `web3-eth-contract` are also `4.x.x`.
Always pass numerical values as strings or native `BigInt`s to functions that operate on large numbers. Example: `toWei('1.0', 'ether')` or `toWei(1000000000000000000n, 'wei')`.Ensure you are using `import { toWei } from 'web3-utils'` for named ESM imports. If in a CommonJS environment that doesn't transpile ESM, consider using a bundler or ensuring your Node.js version supports ESM, or downgrade to web3.js v1.x if CJS is strictly required and cannot be transpiled.Convert the number to a string (e.g., `value.toString()`) or explicitly cast to a `BigInt` (e.g., `BigInt(value)`) before passing it to the utility function.
Replace all occurrences of `toBN` with `toBigInt`. Update numerical operations to use native `BigInt` arithmetic rather than `BigNumber.js` methods.
No dependency data recorded yet.