Invalid JSON RPC response: undefined when calling web3.eth.getBlockNumber without a provider
23K reputation · 21 Sept 2023, 18:53 UTC
When initializing a Web3 instance without assigning a provider, any JSON‑RPC call such as web3.eth.getBlockNumber() throws an error that signals an invalid JSON RPC response (e.g., "Invalid JSON RPC response: undefined").
The error originates from the provider layer’s internal send method, which returns an undefined response when no transport is bound, and the error‑formatting routine treats this as a failure. This behavior is consistent across web3.js 1.x, 2.x and the current 4.x series, indicating a deliberate design choice rather than a version‑specific bug.
The unresolved decision is whether the library should automatically supply a fallback in‑memory provider (for example, a mock) to make scripts repeatable out of the box, or whether it should continue to require explicit provider assignment so that developers notice missing configuration early.
What are the trade‑offs of providing a default mock provider versus enforcing explicit provider assignment in terms of developer experience and CI reliability?
How could a fallback provider be implemented without masking genuine connection errors or conflicting with frameworks that already inject a provider?