js-libp2p icon indicating copy to clipboard operation
js-libp2p copied to clipboard

Getting started javascript example from the docs does not work.

Open aomini opened this issue 3 years ago • 4 comments

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()

aomini avatar Feb 22 '22 03:02 aomini

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"
    }
}

Rio-Lv avatar Feb 24 '22 13:02 Rio-Lv

Thanks @Rio-Lv , I had the same problem and now my getting started example is working.

elmazzun avatar Mar 02 '22 16:03 elmazzun

Triage notes: @Rio-Lv could you open PR to fix this?

mpetrunic avatar Apr 05 '22 15:04 mpetrunic

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

elvinest avatar Apr 15 '22 10:04 elvinest

We now test the examples, this should no longer be relevant.

maschad avatar Sep 28 '23 20:09 maschad