'Error when using bitcore-explorer with bitcore-lib (Bitcore)
I am trying to use bitcore-lib to generate bitcoin address and fetch unspent transaction using bitcore-explorer.. to generate the address here is the code:
var bitcore = require('bitcore-lib');
var rand_buffer = bitcore.crypto.Random.getRandomBuffer(32);
var rand_number = bitcore.crypto.BN.fromBuffer(rand_buffer);
var privateKay = new bitcore.PrivateKey(rand_number);
var privateKeyWif = privateKay.toWIF();
var address = privateKay.toAddress('testnet');
console.log({
rand_buffer:rand_buffer,
rand_number_hex:rand_number,
rand_number_dec:rand_number.toString(),
privateKey:privateKay,
privateKeyWif: privateKeyWif,
address:address,
});
Which is working fine... the output is:
{ rand_buffer: <Buffer 55 8b 27 c4 51 87 97 17 9a 7d 1d 72 48 26 e5 83 95 74 5b 3b b1 b4 b5 b6 a7 1c df 9f 18 e6 97 2e>,
rand_number_hex: <BN: 558b27c4518797179a7d1d724826e58395745b3bb1b4b5b6a71cdf9f18e6972e>,
rand_number_dec: '38692458332424984226826540178179935156087120588336482991409403810055901845294',
privateKey: <PrivateKey: 558b27c4518797179a7d1d724826e58395745b3bb1b4b5b6a71cdf9f18e6972e, network: livenet>,
privateKeyWif: 'Kz5zkBwfiYNkyswsKjot4wWmxHWUZdVMmxf65Z5wLk29ufhxnnQT',
address: <Address: msTDjA4PmyePSWx2VcaQWoWoQ7gWzU2Kqx, type: pubkeyhash, network: testnet> }
after doing any transaction on the generated address, i need to use bitcore-explorers so i requires bitcore-explorers here is the code:
var Insight = require('bitcore-explorers').Insight;
var insight = new Insight('testnet');
insight.getUnspentUtxos(address1,(error,utxos)=>{
if(error) return console.log(error);
console.log(utxos)
});
The problem is when i require bitcore-explorers it gives me following error:
D:\RAHEEL\Projects\gateway\node_modules\bitcore-explorers\node_modules\bitcore-lib\index.js:12
throw new Error(message);
^
Error: More than one instance of bitcore-lib found. Please make sure to require bitcore-lib and check that submodules do not
also include their own bitcore-lib dependency.
at Object.bitcore.versionGuard (D:\RAHEEL\Projects\gateway\node_modules\bitcore-explorers\node_modules\bitcore-lib\index.
js:12:11)
at Object.<anonymous> (D:\RAHEEL\Projects\gateway\node_modules\bitcore-explorers\node_modules\bitcore-lib\index.js:15:9)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:\RAHEEL\Projects\gateway\node_modules\bitcore-explorers\lib\models\addressinfo.js:3:15)
Solution 1:[1]
Yes, at this time, this still seems to be ongoing (and contentious). It has been raised multiple times in Github
I ran into the same issue albeit with slightly different requirements: I am using the bitocore-p2p
npm package; which is currently at version 1.1.2 and requires bitcore-lib
version 0.14.0 as a dependency.
I have preferred not to patch bitcore-p2p/node_modules/bitcore-lib/index.js
(per another answer here and in the github bitcore issues). Instead, in my project's package.json
I maintain a single bitocore-p2p
dependency and then reference its (one-and-only) v0.14.0 bitcore-lib
dependency:
var p2p = require('bitcore-p2p'); //p2p exports
var p2pMod = require.cache[require.resolve('bitcore-p2p')]; //p2p module
var bitcore = p2pMod.require('bitcore-lib'); //p2p/bitcore-lib exports
Or one could use a more fragile approach:
var p2p = require('bitcore-p2p'); //p2p exports
var bitcore = require('bitcore-p2p/node_modules/bitcore-lib'); //p2p/bitcore-lib exports
In my case, this is not problematic. But clearly if I were to require say, the version 0.16.0 bitcore-lib
I would ordinarily want to make that a direct dependency of my project and then run into trouble.
Solution 2:[2]
There is a temp solution here.
~/bitcore-explorers/node_modules/bitcore-lib/index.js
line 7:
bitcore.versionGuard = function(version) {
Change it to:
bitcore.versionGuard = function(version) { return;
However this issue should be fixed for main branch which is not yet fixed currently. You can check this here
Solution 3:[3]
Try adding
"postinstall": "find ./node_modules/**/node_modules -type d -name 'bitcore-lib' -exec rm -r {} + && echo 'Deleted duplicate bitcore-libs'"
in scripts your package.json file. It worked for me.
Solution 4:[4]
I fixed this problem by using
var bitcore = require('bitcore-explorers/node_modules/bitcore-lib');
So, it doesn't create any errors now.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | halfer |
Solution 2 | ogw_yuya |
Solution 3 | Muhammad Waqar |
Solution 4 | nicolsondsouza |