1

I am following this tutorial of scala.js

http://www.scala-js.org/tutorial/basic/

Right now my build.sbt file looks like

enablePlugins(ScalaJSPlugin)
name := "ScalaJSTut1"
version := "1.0"
scalaVersion := "2.12.4"
scalaJSUseMainModuleInitializer := true
libraryDependencies ++= Seq(
   "be.doeraene" %%% "scalajs-jquery" % "0.9.1"
)
skip in packageJSDependencies := false
jsDependencies +=
   "org.webjars" % "jquery" % "2.1.4" / "2.1.4/jquery.js"
jsEnv := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv()

I also successfully executed

npm install jsdom

my index.html looks like

<!DOCTYPE html>
<html>
    <head>
        <meta CHARSET="UTF-8" />
        <title>MY FIRST SCALA.JS APPLICATION</title>
    </head>
    <body>
    <script type="text/javascript" src="http://cdn.jsdelivr.net/jquery/2.1.1/jquery.js"></script>
    <script type="text/javascript" src="./target/scala-2.12/scalajstut1-fastopt.js"></script>
    <script type="text/javascript" src="./target/scala-2.12/scalajstut1-jsdeps.js"></script>
        <button id="click-me-button" type="button">
            Click me!
        </button>
    </body>
</html>

and finally TutorialApp.scala

package tutorial.webapp

import org.scalajs.jquery.jQuery
import scala.scalajs.js.annotation.JSExportTopLevel

object TutorialApp {
   def main(args: Array[String]) : Unit = {
      jQuery(() => setupUI())
   }

   def appendPar(text: String) : Unit = {
      jQuery("body").append(s"<p>$text</p>")
   }

   @JSExportTopLevel("addClickedMessage")
   def addClickedMessage() : Unit = {
      appendPar("You clicked the button")
   }

   def setupUI() : Unit = {
      appendPar("Hello World")
      jQuery("#click-me-button").click(() => addClickedMessage())
   }
}

but now when I do sbt run I get an error

[error] /Users//IdeaProjects/ScalaJSTut1/node_modules/jsdom/lib/api.js:10
[error] const { URL } = require("whatwg-url");
[error]       ^
[error] 
[error] SyntaxError: Unexpected token {
[error]     at exports.runInThisContext (vm.js:53:16)
[error]     at Module._compile (module.js:374:25)
[error]     at Object.Module._extensions..js (module.js:405:10)
[error]     at Module.load (module.js:344:32)
[error]     at Function.Module._load (module.js:301:12)
[error]     at Module.require (module.js:354:17)
[error]     at require (internal/module.js:12:17)
[error]     at [stdin]:39:13
[error]     at [stdin]:67:3
[error]     at Object.exports.runInThisContext (vm.js:54:17)
org.scalajs.jsenv.ExternalJSEnv$NonZeroExitException: Node.js with JSDOM exited with code 1
        at org.scalajs.jsenv.ExternalJSEnv$AbstractExtRunner.waitForVM(ExternalJSEnv.scala:119)
        at org.scalajs.jsenv.ExternalJSEnv$ExtRunner.run(ExternalJSEnv.scala:168)
        at org.scalajs.sbtplugin.ScalaJSPluginInternal$.org$scalajs$sbtplugin$ScalaJSPluginInternal$$jsRun(ScalaJSPluginInternal.scala:787)
        at org.scalajs.sbtplugin.ScalaJSPluginInternal$$anonfun$76$$anonfun$apply$51$$anonfun$apply$52.apply(ScalaJSPluginInternal.scala:939)
        at org.scalajs.sbtplugin.ScalaJSPluginInternal$$anonfun$76$$anonfun$apply$51$$anonfun$apply$52.apply(ScalaJSPluginInternal.scala:933)
        at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) org.scalajs.jsenv.ExternalJSEnv$NonZeroExitException: Node.js with JSDOM exited with code 1
[error] Total time: 2 s, completed Nov 5, 2017 11:16:58 PM
~/IdeaProjects/ScalaJSTut1 > 

when I do nom --version on my computer I see

npm --version 3.3.12

1 Answer 1

1

I was able to resolve the issue. The problem is that my npm version was outdated.

I did the following

brew install nvm
nvm install node

Now my npm --version shows

~/IdeaProjects/ScalaJSTut1 > npm --version
5.5.1

This resolved the problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.