holepunchto / pear

combined Peer-to-Peer (P2P) Runtime, Development & Deployment tool
https://docs.pears.com/
Apache License 2.0
91 stars 9 forks source link

ModuleError: MODULE_NOT_FOUND: Cannot find module 'node:process' #61

Closed dabuchera closed 1 month ago

dabuchera commented 5 months ago

I think this has already been discussed in the chat. Therefore just for documentation.

Using an external package which is using the global node:process gives the following error:

ModuleError: MODULE_NOT_FOUND: Cannot find module 'node:process' imported from 'pear://dev/node_modules/@clack/prompts/dist/index.mjs'

Using here @clack/promps

kasperisager commented 5 months ago

We have a compatibility layer for Node.js builtins that you can use: https://github.com/holepunchto/bare-node

dabuchera commented 3 months ago

Hey @kasperisager Thank you! The error message no longer appears. Nevertheless, a strange behavior can now be observed. Running pear dev . starts without an error message but nothing happens and it is terminated again. If import { cancel, isCancel, select } from '@clack/prompts' is commented out everything works find.

import b4a from 'b4a'
import readline from 'bare-readline'
import tty from 'bare-tty'
import crypto from 'hypercore-crypto'
import Hyperswarm from 'hyperswarm'

import { cancel, isCancel, select } from '@clack/prompts'

const { teardown, config } = Pear // Import configuration options and cleanup functions from Pear
const key = config.args.pop() // Retrieve a potential chat room key from command-line arguments
const shouldCreateSwarm = !key // Flag to determine if a new chat room should be created
const swarm = new Hyperswarm()

teardown(() => swarm.destroy())

const rl = readline.createInterface({
  input: new tty.ReadStream(0),
  output: new tty.WriteStream(1),
})

swarm.on('connection', (peer) => {
  const name = b4a.toString(peer.remotePublicKey, 'hex').substr(0, 6)
  console.log(`[info] New peer joined, ${name}`)
  peer.on('data', (message) => appendMessage({ name, message }))
  peer.on('error', (e) => console.log(`Connection error: ${e}`))
})

swarm.on('update', () => {
  console.log(`[info] Number of connections is now ${swarm.connections.size}`)
})

if (shouldCreateSwarm) {
  await createChatRoom()
} else {
  await joinChatRoom(key)
}

rl.input.setMode(tty.constants.MODE_RAW)
rl.on('data', (line) => {
  sendMessage(line)
  rl.prompt()
})
rl.prompt()

async function createChatRoom() {
  const topicBuffer = crypto.randomBytes(32)
  await joinSwarm(topicBuffer)
  const topic = b4a.toString(topicBuffer, 'hex')
  console.log(`[info] Created new chat room: ${topic}`)
}

async function joinChatRoom(topicStr) {
  const topicBuffer = b4a.from(topicStr, 'hex')
  await joinSwarm(topicBuffer)
  console.log(`[info] Joined chat room`)
}

async function joinSwarm(topicBuffer) {
  const discovery = swarm.join(topicBuffer, { client: true, server: true })
  await discovery.flushed()
}

function sendMessage(message) {
  const peers = [...swarm.connections]
  for (const peer of peers) peer.write(message)
}

function appendMessage({ name, message }) {
  console.log(`[${name}] ${message}`)
}
kasperisager commented 3 months ago

Pleasure! I think error handling in Pear might still be wonky, see #89. Loading @clack/prompts directly in Bare revealed that tty.isatty() was missing, I've added that here: https://github.com/holepunchto/bare-tty/commit/4e56a1386705a44ee2e7c2e1eac56a7e8b88b19b. Let me know if it does the trick!

dabuchera commented 3 months ago

Nice. Thanks for having a look. Unfortunately, it doesn't work yet. Any approaches i could try out?

kasperisager commented 3 months ago

I'd try removing all the Pear bits to hopefully isolate the issue to either Bare or Pear.

dabuchera commented 3 months ago

Most likely Bare

davidmarkclements commented 1 month ago

closing as resolve per topic