Ethjs Ethereum Utility Library
Ethjs is a lightweight, highly optimized JavaScript utility library designed for interacting with the Ethereum blockchain. Released in its current stable version 0.4.0 in 2018, it aimed to provide a more modular and async-only alternative to the then-dominant `web3.js` library, featuring `BN.js` for large number arithmetic and a focus on smaller bundle sizes. However, the project has been unmaintained since its last publish nearly eight years ago. Developers should be aware that `ethjs` is effectively abandoned; its GitHub repository explicitly recommends using `ethers.js` instead. Modern Ethereum development typically leverages actively maintained libraries like `ethers.js` or the `@ethereumjs` monorepo, which offer contemporary features such as ESM support, TypeScript definitions, and native JavaScript `BigInt` for handling large numbers, providing a more robust and future-proof development experience.
Common errors
-
Error: Cannot find module 'ethjs' imported from ...
cause Attempting to import `ethjs` using ES module `import` syntax in an environment where only CommonJS `require` is supported, or when `ethjs` is not correctly installed.fixChange your import statement to `const Eth = require('ethjs');`. Ensure `ethjs` is listed in your `package.json` dependencies and installed (`npm install ethjs`). -
TypeError: Eth.toWei is not a function
cause Incorrectly destructuring or accessing static methods, or calling a method on an instance when it should be called statically.fixEnsure `Eth` is imported as the main module (`const Eth = require('ethjs');`) and static methods like `toWei` are called directly on `Eth` (e.g., `Eth.toWei(value, unit)`). Do not attempt `const { toWei } = require('ethjs');`.
Warnings
- breaking The `ethjs` library is effectively unmaintained since its last release (v0.4.0) in 2018. It is strongly recommended to use actively maintained alternatives like `ethers.js` or packages from the `@ethereumjs` monorepo for any new development or migration of existing projects.
- gotcha Ethjs relies on the `BN.js` library for handling large numbers. Modern JavaScript environments and newer Ethereum libraries have largely transitioned to native `BigInt` for arbitrary-precision integers. Mixing `BN.js` objects with `BigInt` or standard JavaScript numbers can lead to unexpected behavior and errors.
- gotcha Ethjs is a CommonJS-only library, meaning it can only be imported using `require()` syntax in Node.js environments. It does not provide native ES module (ESM) support (`import`). Attempting to use `import` statements will result in module resolution errors.
- gotcha Ethjs employs a mixed asynchronous API, with some methods (like `getBlockByNumber`) using Node.js-style callbacks, while others (like contract calls) return Promises. This inconsistency can make error handling and control flow more complex.
Install
-
npm install ethjs -
yarn add ethjs -
pnpm add ethjs
Imports
- Eth
import Eth from 'ethjs';
const Eth = require('ethjs'); - Eth.HttpProvider
import { HttpProvider } from 'ethjs'; const provider = new HttpProvider(...);const Eth = require('ethjs'); const provider = new Eth.HttpProvider('https://mainnet.infura.io'); - Eth.toWei
import { toWei } from 'ethjs'; const weiValue = toWei(...);const Eth = require('ethjs'); const weiValue = Eth.toWei(1, 'ether');
Quickstart
const Eth = require('ethjs');
const eth = new Eth(new Eth.HttpProvider('https://ropsten.infura.io'));
// Fetch a block by number using a callback (common in older Node.js libs)
eth.getBlockByNumber(45300, true, (err, block) => {
if (err) {
console.error('Error fetching block:', err);
return;
}
console.log('Block 45300 data:', block.number.toString(), block.hash);
});
// Convert ether to wei using Eth.toWei utility
const etherValue = Eth.toWei(72, 'ether');
console.log('72 Ether in Wei (BN.js object):', etherValue.toString());
// Interact with a contract using a promise-based API
const tokenABI = [{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs":[{"name": "","type": "uint256"}],
"payable": false,
"type": "function"
}];
// This contract address is an example and may not be valid on Ropsten today
const tokenAddress = '0x6e0E0e02377Bc1d90E8a7c21f12BA385C2C35f78';
const token = eth.contract(tokenABI).at(tokenAddress);
token.totalSupply()
.then((totalSupply) => {
console.log(`Total supply for token at ${tokenAddress}: ${totalSupply.toString()}`);
})
.catch(error => {
console.error(`Error fetching total supply for token at ${tokenAddress}:`, error);
});