When creating a Web3 instance and then attempting to call setProvider with a valid provider, a TypeError occurs.
Calling setProvider should successfully set the provider without an error.
A TypeError occurs (see error logs below).
const Web3 = require("web3");
const web3 = new Web3();
web3.setProvider(new Web3.providers.HttpProvider(someValidUrl));
TypeError: Cannot read property 'clearSubscriptions' of undefined
at Web3.clearSubscriptions (/Users/name/projects/truffle/node_modules/web3-core/dist/web3-core.cjs.js:64:39)
at Web3.setProvider (/Users/name/projects/truffle/node_modules/web3-core/dist/web3-core.cjs.js:44:14)
at Web3.setProvider (/Users/name/projects/truffle/node_modules/web3/dist/web3.cjs.js:41:73)
I've couldn't reproduce this issue with the provided example. Could you reference the complete code as GitHub repository or gist file?
So to give a slightly more verbose version of the above steps...so I start Ganache running at port 9545 and do the following:
const Web3 = require("web3");
const provider = new Web3.providers.HttpProvider("http://localhost:9545");
const web3 = new Web3();
web3.setProvider(provider);
It makes sense when looking at web3-core.cjs.js here. If a web3 instance does not have a provider it will throw when you try to set it because it calls clearSubscriptions in web3-core.cjs.js. This method checks this.currentProvider.clearSubscriptions, but this.currentProvider is undefined and will throw.
Is it not also possible to initiate Web3 with a non-existing URL?
const Web3 = require("web3");
const provider = new Web3.providers.HttpProvider("http://localhost:9545");
const web3 = new Web3('http://');
web3.setProvider(provider);
Well yes, I assume that would work...but isn't that a bit janky?
Yes, this is true. It is probably better if I update the setProvider method. I've checked the truffle-contract package and truffle does copy the methods from the Web3.js Contract class to a truffle related class. I think this could give some other problems. Is it possible for truffle to create a list and we could go through the list?
Sure! What kind of a list would you like to compile exactly? Right now I am not too sure that I am completely aware of all of the problems that Truffle currently has with the newest versions of Web3.
I know there is some work that we need to do to adapt to the new provider standard for sure. I'll try to get as much of the work that I know needs to be done and then take another look.
I also know the "strings to BN" change is going to be a big deal for Truffle since that is a breaking change and we just released a major version not too long ago.
In other words, I don't think we can upgrade to the latest version without that string functionality. Maybe we could talk about some possible solutions?
Sure! What kind of a list would you like to compile exactly?
I think about a list where all the problems of truffle are listed would support the discussion :)
I know there is some work that we need to do to adapt to the new provider standard for sure.
The EIP-1193 isn't finalized and the CustomProvider fallback was created with the given interface from truffle. Was there anything missing in the given interface?
I think the biggest problem is that truffle does copy methods from the Web3 contract class to a custom truffle object with no relation to a Web3 API. Truffle could wrap these methods or it could use the ES6 proxy for providing the same truffle-contract API (adapter pattern).
I also know the "strings to BN" change is going to be a big deal for Truffle since that is a breaking change and we just released a major version not too long ago.
Is it possible to have deeper insights? (code references etc.)
Edit: Adapter Embark
Let me talk with the Truffle team and see what information we can put together. To be honest I'm not sure how complete our knowledge is about what is currently incompatible with the latest versions of Web3.
What do you suggest if I'm using Ganache as a provider?
var Web3 = require("web3");
var web3 = new Web3();
web3.setProvider(ganache.provider());
Hello,
I encountered the same issue. I'm only using Ganache as provider, running it on a server in my local network, and I was launching my node instance with this (not a Truffle project) :
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider("http://10.0.0.42:7545"));
Then I got the following error :
path/to/my/project/node_modules/web3-core/dist/web3-core.cjs.js:64
if (typeof this.currentProvider.clearSubscriptions !== 'undefined' && this.currentProvider.subscriptions.length > 0) {
^
TypeError: Cannot read property 'clearSubscriptions' of undefined
...
But when I do it this way, it works :
const Web3 = require('web3');
const ethprovider = new Web3.providers.HttpProvider("http://10.0.0.42:7545");
const web3 = new Web3('http://');
web3.setProvider(ethprovider);
nodejs v10.15.3
web3js 1.0.0-beta.55
This got fixed in the 2.x branch and 1.x doesn't have that issue.
Most helpful comment
Hello,
I encountered the same issue. I'm only using Ganache as provider, running it on a server in my local network, and I was launching my node instance with this (not a Truffle project) :
Then I got the following error :
But when I do it this way, it works :
nodejs v10.15.3
web3js 1.0.0-beta.55