Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting Started js-libp2p: ERR_PACKAGE_PATH_NOT_EXPORTED #361

Open
jehaverlack opened this issue Jan 27, 2024 · 0 comments
Open

Getting Started js-libp2p: ERR_PACKAGE_PATH_NOT_EXPORTED #361

jehaverlack opened this issue Jan 27, 2024 · 0 comments

Comments

@jehaverlack
Copy link

jehaverlack commented Jan 27, 2024

I'm attempting to follow the direction at:

$ cat /etc/debian_version 
12.4

$ node --version
v20.11.0

But I keep getting this error:

$ node src/index.js 
node:internal/modules/esm/resolve:303
  return new ERR_PACKAGE_PATH_NOT_EXPORTED(
         ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './ping' is not defined by "exports" in /home/jehaverlack/Documents/Demos/hello-libp2p/node_modules/libp2p/package.json imported from /home/jehaverlack/Documents/Demos/hello-libp2p/src/index.js
    at exportsNotFound (node:internal/modules/esm/resolve:303:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:650:9)
    at packageResolve (node:internal/modules/esm/resolve:836:14)
    at moduleResolve (node:internal/modules/esm/resolve:910:20)
    at defaultResolve (node:internal/modules/esm/resolve:1130:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
    at link (node:internal/modules/esm/module_job:84:36) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Node.js v20.11.0

Here is my src/index.js and package.json

src/index.js

import process from 'node:process'
import { createLibp2p } from 'libp2p'
import { tcp } from '@libp2p/tcp'
import { noise } from '@chainsafe/libp2p-noise'
import { mplex } from '@libp2p/mplex'
import { multiaddr } from 'multiaddr'
import { pingService } from 'libp2p/ping'

const node = await createLibp2p({
  addresses: {
    // add a listen address (localhost) to accept TCP connections on a random port
    listen: ['/ip4/127.0.0.1/tcp/0']
  },
  transports: [tcp()],
  connectionEncryption: [noise()],
  streamMuxers: [mplex()],
  services: {
    ping: pingService({
      protocolPrefix: 'ipfs', // default
    }),
  },
})

// start libp2p
await node.start()
console.log('libp2p has started')

// print out listening addresses
console.log('listening on addresses:')
node.getMultiaddrs().forEach((addr) => {
  console.log(addr.toString())
})

// ping peer if received multiaddr
if (process.argv.length >= 3) {
  const ma = multiaddr(process.argv[2])
  console.log(`pinging remote peer at ${process.argv[2]}`)
  const latency = await node.services.ping.ping(ma)
  console.log(`pinged ${process.argv[2]} in ${latency}ms`)
} else {
  console.log('no remote peer address given, skipping ping')
}

const stop = async () => {
  // stop libp2p
  await node.stop()
  console.log('libp2p has stopped')
  process.exit(0)
}

process.on('SIGTERM', stop)
process.on('SIGINT', stop)

package.json

{
  "name": "",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "type": "module",
  "license": "AGPL-version-3.0",
  "private": false,
  "engines": {
    "node": ">= 14.0.0",
    "npm": ">= 6.0.0"
  },
  "homepage": "",
  "repository": {
    "type": "git",
    "url": ""
  },
  "bugs": "",
  "keywords": [],
  "author": {
    "name": "",
    "email": "",
    "url": ""
  },
  "contributors": [],
  "scripts": {
    "dev": "",
    "test": "",
    "start": "node src/index.js"
  },
  "dependencies": {
    "@chainsafe/libp2p-noise": "^15.0.0",
    "@libp2p/mplex": "^10.0.12",
    "@libp2p/tcp": "^9.0.12",
    "libp2p": "^1.2.0",
    "multiaddr": "^10.0.1"
  },
  "devDependencies": {

  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant