0

I'm new to ScalaJS, so I'm a little perplexed with this issue.

I'm trying to write a very simple facade for the peer.js library. I have this:

@js.native
@JSGlobal
class Peer() extends js.Object {
  def this(id: String = ???, options: js.Object = ???) = this()
  def connect(id: String, options: js.Object = ???): DataConnection = js.native
  def on(event: String, callback: js.Function): Unit                = js.native
  def disconnect(): Unit                                            = js.native
  def reconnect(): Unit                                             = js.native
  def destroy(): Unit                                               = js.native
  def id: String                                                    = js.native
  def connections: js.Object                                        = js.native
  def disconnected: Boolean                                         = js.native
  def destroyed: Boolean                                            = js.native
}

And here is the simple code I'm trying to run:

object index {
  def main(args: Array[String]): Unit = {
    val peer = new Peer()
    peer.on("open", (id: String) => println(id))
  }
}

This small piece of code works perfectly fine in the browser, however when I try to run it in the sbt shell, I get this error:

ReferenceError: Peer is not defined
ReferenceError: Peer is not defined
    at $c_Lcom_nicolaswinsten_peerscalajs_index$.main__AT__V (file:///C:/Users/mjwin/IdeaProjects/peer-scalajs/target/scala-2.13/peer-scalajs-fastopt/main.js:840:14)
    at $s_Lcom_nicolaswinsten_peerscalajs_index__main__AT__V (file:///C:/Users/mjwin/IdeaProjects/peer-scalajs/target/scala-2.13/peer-scalajs-fastopt/main.js:826:47)
    at file:///C:/Users/mjwin/IdeaProjects/peer-scalajs/target/scala-2.13/peer-scalajs-fastopt/main.js:2078:1
    at file:///C:/Users/mjwin/IdeaProjects/peer-scalajs/target/scala-2.13/peer-scalajs-fastopt/main.js:2079:4
    at Script.runInContext (vm.js:143:18)
    at Object.runInContext (vm.js:294:6)
    at processJavaScript (C:\Users\mjwin\IdeaProjects\peer-scalajs\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:241:10)
    at HTMLScriptElementImpl._innerEval (C:\Users\mjwin\IdeaProjects\peer-scalajs\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:176:5)
    at onLoadExternalScript (C:\Users\mjwin\IdeaProjects\peer-scalajs\node_modules\jsdom\lib\jsdom\living\nodes\HTMLScriptElement-impl.js:98:12)
    at onLoadWrapped (C:\Users\mjwin\IdeaProjects\peer-scalajs\node_modules\jsdom\lib\jsdom\browser\resources\per-document-resource-loader.js:53:33)
[error] org.scalajs.jsenv.ExternalJSRun$NonZeroExitException: exited with code 1
[error]         at org.scalajs.jsenv.ExternalJSRun$$anon$1.run(ExternalJSRun.scala:186)
[error] stack trace is suppressed; run 'last Compile / run' for the full output
[error] (Compile / run) org.scalajs.jsenv.ExternalJSRun$NonZeroExitException: exited with code 1

I'm sure it's something really simple, but I'm stumped. Any guesses? Thank you

nick
  • 119
  • 4
  • What does your configuration look like? How did you tell the browser where to find the library? And how did you you tell sbt where to find it? – sjrd Mar 30 '21 at 23:36
  • @sjrd Oh that makes perfect sense. I just have the library sourced in a script tag in index.html but I haven't told sbt how to find it. How would I do that? Could you point me somewhere? – nick Mar 31 '21 at 01:18
  • You can use https://scalacenter.github.io/scalajs-bundler/ for this – Nikita Mar 31 '21 at 05:43
  • The key word to use for search is `jsEnvInput`. I'll come back later with a proper answer, as I'm on mobile right now. – sjrd Mar 31 '21 at 06:04
  • @Nikita Thanks! I'm still having trouble though. I get an `jsenv.UnsupportedInputException` in the shell and now the browser is still saying `Peer is not defined`. Thanks anyway though! – nick Mar 31 '21 at 19:16
  • @nick I'm not sure but sounds like https://stackoverflow.com/questions/63958419/scalajs-env-jsdom-nodejs-run-fails-with-unsupportedinputexception maybe? See comments there too. – Nikita Mar 31 '21 at 19:20
  • Thank you @Nikita, I actually read that one and it didn't give me much, however I tried jsDependencies, and sbt seemed to find my dependency however now I just need to figure out how to emit my dependency and my code together. – nick Mar 31 '21 at 19:42

0 Answers0