js-libp2p
js-libp2p copied to clipboard
Getting started javascript example from the docs does not work.
The issue is in the package build of libp2p-mplex.
The code below from the official tutorial results into error. The error message is Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in C:\[...]\node_modules\libp2p-mplex\package.json
.
Versions
- "node": 16.14.0
- "libp2p": "^0.36.2",
- "libp2p-mplex": "^1.0.0",
This is probably happening because the package.json of libp2p-mplex
only contains the resolution for imports no for require.
"exports": {
".": {
"import": "./dist/src/index.js"
}
}
So, for now I resolved the issue by using es6 modules rather than common js.
const Libp2p = require('libp2p')
const TCP = require('libp2p-tcp')
const { NOISE } = require('libp2p-noise')
const MPLEX = require('libp2p-mplex')
const main = async () => {
const node = await Libp2p.create({
addresses: {
// add a listen address (localhost) to accept TCP connections on a random port
listen: ['/ip4/127.0.0.1/tcp/0']
},
modules: {
transport: [TCP],
connEncryption: [NOISE],
streamMuxer: [MPLEX]
}
})
// start libp2p
await node.start()
console.log('libp2p has started')
// print out listening addresses
console.log('listening on addresses:')
node.multiaddrs.forEach(addr => {
console.log(`${addr.toString()}/p2p/${node.peerId.toB58String()}`)
})
// stop libp2p
await node.stop()
console.log('libp2p has stopped')
}
main()
I changed some dependency versions in package.json these ones seems to work so far
{
"name": "whatevername",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"libp2p": "^0.34.0",
"libp2p-mplex": "^0.10.7",
"libp2p-noise": "^4.0.0",
"libp2p-tcp": "^0.17.2",
"multiaddr": "^10.0.1"
}
}
Thanks @Rio-Lv , I had the same problem and now my getting started example is working.
Triage notes: @Rio-Lv could you open PR to fix this?
I have a problem with libp2p-mdns
(https://github.com/libp2p/js-libp2p/blob/master/examples/discovery-mechanisms/2.js)
const MulticastDNS=require('libp2p-mdns'); peerDiscovery:[ new MulticastDNS({ interval: 2000 }) ]
throw new Error('needs own PeerId to work')
I checked on github the version is 1.0.4 (https://github.com/libp2p/js-libp2p-mdns/blob/master/package.json) and the version I installed is 0.18.0 (npm install libp2p-mdns)
I tried downloading the version on github but it doesn't work.
const MulticastDNS=require('@libp2p/mdns');
thank you
We now test the examples, this should no longer be relevant.