Ethjs HTTP Provider
ethjs-provider-http is a minimalist HTTP provider module for interacting with the Ethereum RPC layer, designed to adhere to the `web3` provider specification. It uses the `xhr2` module for its underlying HTTP requests. Released in 2016 and last updated to version 0.1.6 in January 2017, this package is part of the broader `ethjs` ecosystem, which aimed to provide a lightweight alternative to `web3.js` at the time. However, the `ethjs` suite is no longer actively maintained and has largely been superseded by modern Ethereum client libraries such as Ethers.js and the latest versions of Web3.js. There is no ongoing release cadence, and it is primarily of historical interest or for maintaining legacy applications that specifically depend on it. Its key differentiator was its simplicity and a modular approach within the `ethjs` family, but this is now overshadowed by its lack of maintenance and modern feature support.
Common errors
-
Error: Invalid JSON RPC response: "<html>..."
cause The RPC endpoint responded with an HTML error page (e.g., 404 Not Found, 403 Forbidden) instead of a valid JSON-RPC payload, often due to an incorrect or deprecated URL, or missing API key.fixVerify the RPC endpoint URL is correct and active. Ensure any required API keys are included in the URL or headers as per the provider's documentation. Check your network connection. -
TypeError: HttpProvider is not a constructor
cause Attempting to use `ethjs-provider-http` after an incorrect import, or in an environment where `require` does not correctly resolve the module.fixEnsure you are using `const HttpProvider = require('ethjs-provider-http');`. If using TypeScript, you might need `const HttpProvider = require('ethjs-provider-http').default;` or custom type declarations if the library doesn't provide them. -
ERR_OSSL_EVP_UNSUPPORTED
cause Older Node.js modules, especially those dealing with cryptography or network protocols, can encounter OpenSSL issues with newer Node.js versions due to changes in cryptographic policies or default algorithms.fixTry running Node.js with `--openssl-legacy-provider` flag (e.g., `node --openssl-legacy-provider your-script.js`). This is a temporary workaround; the long-term solution is to use a modern, updated library.
Warnings
- breaking This package is no longer actively maintained or developed. The last publish to npm was in January 2017, and the underlying `ethjs` ecosystem is largely inactive. It is highly recommended to migrate to modern, actively maintained libraries like Ethers.js or Web3.js for new projects.
- gotcha The example code, and the library itself, often references deprecated Ethereum testnets like Ropsten. These networks are no longer active or supported by most RPC providers, leading to connection failures or incorrect data.
- breaking Compatibility with modern Ethereum JSON-RPC specifications (e.g., EIP-1193 provider interface) and newer client features is not guaranteed due to the package's age and lack of updates.
- gotcha As an unmaintained package, `ethjs-provider-http` may contain unpatched security vulnerabilities. Using it in production environments is not recommended due to potential risks.
- breaking This library is strictly CommonJS (`require`) and does not support ES Modules (`import`). Attempting to use `import` syntax will result in errors.
Install
-
npm install ethjs-provider-http -
yarn add ethjs-provider-http -
pnpm add ethjs-provider-http
Imports
- HttpProvider
import { HttpProvider } from 'ethjs-provider-http';const HttpProvider = require('ethjs-provider-http');
Quickstart
const HttpProvider = require('ethjs-provider-http');
const Eth = require('ethjs-query');
// NOTE: Ropsten is deprecated. Use a current testnet like Sepolia or Goerli,
// or a mainnet RPC endpoint from a service like Infura or Alchemy.
const provider = new HttpProvider('https://mainnet.infura.io/v3/' + (process.env.INFURA_API_KEY ?? ''));
const eth = new Eth(provider);
eth.getBlockByNumber(45039930, true, (err, result) => {
if (err) {
console.error('Error fetching block:', err);
return;
}
if (result) {
console.log('Block data:', result);
} else {
console.log('Block not found or null result.');
}
});