Scala.js basic tutorial SyntaxError: Unexpected token

Scala.js basic tutorial SyntaxError: Unexpected token

我正在学习 scala.js

的教程

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

现在我的 build.sbt 文件看起来像

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()

我也执行成功了

npm install jsdom

我的index.html长得像

<!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>

最后 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())
   }
}

但现在当我这样做时 sbt run 我得到一个错误

[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$$anonfun$apply$$anonfun$apply.apply(ScalaJSPluginInternal.scala:939)
        at org.scalajs.sbtplugin.ScalaJSPluginInternal$$anonfun$$anonfun$apply$$anonfun$apply.apply(ScalaJSPluginInternal.scala:933)
        at scala.Function1$$anonfun$compose.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 > 

当我在我的计算机上执行 nom --version 时,我看到了

npm --version 3.3.12

我能够解决问题。问题是我的 npm 版本已经过时了。

我做了以下事情

brew install nvm
nvm install node

现在我的npm --version显示

~/IdeaProjects/ScalaJSTut1 > npm --version
5.5.1

这解决了问题。