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

Updating instructions in examples/transports tut #1200

Closed
wants to merge 4 commits into from

Conversation

walkerlj0
Copy link

Updated part 1 (1.js)

@BigLep
Copy link
Contributor

BigLep commented May 3, 2022

2022-05-03 conversation: there are two things in this PR:

  1. moving back to CJS
  2. some new/helpful content

We don't want the move back to CJS given we are making js-libp2p ESM-only. We do want the new/helpful content.

@walkerlj0 : if you are wanting to contribute more here, please reduce to #2 only. Otherwise, it will get picked up once the EMS-only js-libp2p is released.

@wemeetagain
Copy link
Member

@walkerlj0 libp2p 0.37.0 has finally been released with ESM-only exports.

Can you try to make the examples run against the latest version?

@wemeetagain wemeetagain added the need/author-input Needs input from the original author label May 16, 2022
Comment on lines +48 to +69
Now that we have a function to create our own libp2p node, let's create a node with it. Inside of the createNode() async function, before the `return node` and after the `await node.start() ` statement, add in some logging:

```JavaScript
const createNode = async () => {
...
await node.start()

console.log('node has started (true/false):', node.isStarted())
console.log('listening on:')
node.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${node.peerId.toB58String()}`))

return node
}
```

Now, you want to call the `createNode()` function, and you can also add in error handling:

```JavaScript
const node = await createNode()

// At this point the node has started
console.log('node has started (true/false):', node.isStarted())
// And we can print the now listening addresses.
// If you are familiar with TCP, you might have noticed
// that we specified the node to listen in 0.0.0.0 and port
// 0, which means "listen in any network interface and pick
// a port for me
console.log('listening on:')
node.multiaddrs.forEach((ma) => console.log(`${ma.toString()}/p2p/${node.peerId.toB58String()}`))
createNode().catch(err => {
console.error(err)
process.exit(1)
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert the changes other than this? We want to keep examples using the latest versions of libp2p dependencies, using ESM imports, etc.

@BigLep
Copy link
Contributor

BigLep commented Jun 7, 2022

@walkerlj0: are you able to incorporate the feedback so the PR can be merged?

@walkerlj0
Copy link
Author

I will try and run it in the next coupld days, so sorry, been swamped!

@mpetrunic mpetrunic marked this pull request as draft June 21, 2022 15:32
@mpetrunic
Copy link
Member

@walkerlj0 we converted it to draft state until you are ready.

@walkerlj0
Copy link
Author

walkerlj0 commented Jun 21, 2022

@mpetrunic tried running transports/1.js and got this error, advice on how to troubleshoot?

**Note I did fetch upstream from libp2p:master

file:///Users/lindsayjowalker/Git/js-libp2p/examples/transports/1.js:3
const Libp2p = require('libp2p')
               ^

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/Users/lindsayjowalker/Git/js-libp2p/examples/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///Users/lindsayjowalker/Git/js-libp2p/examples/transports/1.js:3:16
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

@mpetrunic
Copy link
Member

@mpetrunic tried running transports/1.js and got this error, advice on how to troubleshoot?

**Note I did fetch upstream from libp2p:master

file:///Users/lindsayjowalker/Git/js-libp2p/examples/transports/1.js:3
const Libp2p = require('libp2p')
               ^

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/Users/lindsayjowalker/Git/js-libp2p/examples/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///Users/lindsayjowalker/Git/js-libp2p/examples/transports/1.js:3:16
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

If you fetched latest master, do npm install in root followed by npm run build. Also do npm install in the examples directory. You shouldn't have require statement anywhere, if you have you probably have some local changes or you didn't pull latest libp2p:master.

Gonna close this PR as examples work just fine for me locally!

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

Successfully merging this pull request may close these issues.

4 participants