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 javascript example from the docs does not work with nodejs #1620

Closed
nikumbgo12345 opened this issue Mar 9, 2023 · 4 comments
Labels
need/author-input Needs input from the original author

Comments

@nikumbgo12345
Copy link

getting exception as below when trying to run the example in nodejs setup , i am a new to libp2p world , any help is appreciated

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in/samplenode/node-hello-master/node_modules/@libp2p/mdns/package.json
at new NodeError (node:internal/errors:372:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:472:9)
at packageExportsResolve (node:internal/modules/esm/resolve:693:7)
at resolveExports (node:internal/modules/cjs/loader:482:36)
at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object. (/samplenode/node-hello-master/index.js:2:14) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}


Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in/samplenode/node-hello-master/node_modules/libp2p/package.json
at new NodeError (node:internal/errors:372:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:472:9)
at packageExportsResolve (node:internal/modules/esm/resolve:693:7)
at resolveExports (node:internal/modules/cjs/loader:482:36)
at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object. (/samplenode/node-hello-master/index.js:1:22) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'

class used
app.txt

/samplenode/node-hello-master$ node -v
v16.15.1
samplenode/node-hello-master$ npm -v
9.5.1

package,.json

{
"name": "node-hello",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/johnpapa/node-hello.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/johnpapa/node-hello/issues"
},
"homepage": "https://github.com/johnpapa/node-hello#readme",
"dependencies": {
"@chainsafe/libp2p-noise": "^11.0.1",
"@libp2p/mdns": "^7.0.0",
"@libp2p/mplex": "^7.1.1",
"@libp2p/tcp": "^6.1.2",
"@libp2p/websockets": "^5.0.4",
"libp2p": "^0.42.2"
}
}

@nikumbgo12345 nikumbgo12345 added the need/triage Needs initial labeling and prioritization label Mar 9, 2023
@nikumbgo12345 nikumbgo12345 changed the title Getting started javascript example from the docs does not work. Getting started javascript example from the docs does not work with nodejs Mar 9, 2023
@achingbrain
Copy link
Member

You cannot use require to load libp2p, it is an ESM-only module. Please use import or if you have to use CJS for whatever reason, use a dynamic import().

@achingbrain achingbrain added need/author-input Needs input from the original author and removed need/triage Needs initial labeling and prioritization labels Mar 10, 2023
@nikumbgo12345
Copy link
Author

the issue seems compatibility related , the library does not work with 16.0.0 however started working with node version 18 that also only 18.0.0, so it would be nice to share with the first time users the exact version on node and npm when we test a particular release

@nikumbgo12345 nikumbgo12345 closed this as not planned Won't fix, can't repro, duplicate, stale Mar 12, 2023
@fcbrandon
Copy link

Bringing this back up rather than creating a new ticket.

The issue is that the example documentation is errant how it's currently written: https://github.com/libp2p/js-libp2p/tree/master/examples/pubsub

1.js uses import, but the README shows the example being run as such:

> node 1.js
connected to QmWpvkKm6qHLhoxpWrTswY6UMNWDyn8hN265Qp9ZYvgS82
node1 received: Bird bird bird, bird is the word!
node1 received: Bird bird bird, bird is the word!

I'm on Node.js v19.8.1.

@jehaverlack
Copy link

I think I'm seeing the same issue:
libp2p/docs#361

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need/author-input Needs input from the original author
Projects
None yet
Development

No branches or pull requests

4 participants