TypeError: jsdom.createVirtualConsole is not a function

TypeError: jsdom.createVirtualConsole is not a function

我正在尝试构建 basic Scala.js tutorial,但遇到了这个奇怪的错误。

与教程中所示的项目设置没有太大区别,但以防万一这是我的 build.sbt:

enablePlugins(ScalaJSPlugin)

scalaVersion := "2.12.1"

name := "algorithms1_4_34"
version := "1.0"

libraryDependencies ++= Seq("org.scalatest" % "scalatest_2.12" % "3.0.1" % "test",
            "org.scalacheck" %% "scalacheck" % "1.13.4" % "test",
             "org.scala-js" % "scalajs-dom_sjs0.6_2.12" % "0.9.1",
             "be.doeraene" %%% "scalajs-jquery" % "0.9.1")

// This is an application with a main method
scalaJSUseMainModuleInitializer := true

skip in packageJSDependencies := false
jsDependencies +=
  "org.webjars" % "jquery" % "2.1.4" / "2.1.4/jquery.js"
jsDependencies += RuntimeDOM

...和 ​​JSApp 文件:

package ca.vgorcinschi.algorithms1_4_34

import scala.scalajs.js.JSApp
import org.scalajs.jquery.jQuery

object HotAndColdJS extends JSApp{

   def main(): Unit = {
     jQuery(()=>setupUI())
  }

  def addClickedMessage():Unit ={
    jQuery("body").append("<p>You clicked the button!</p>")
  }

  def setupUI():Unit = {
    //click envokes an event handler
    jQuery("#click-me-button").click(()=> addClickedMessage())
    jQuery("body").append("<p>Hello World!</p>")
  }
}

我可以 运行 compilefastOptJSreloadeclipse(我正在使用 eclipsePlugin)命令没有问题。唯一的问题是 run 命令。公平地说,我确实在教程的流程中添加了一些内容,但这只是因为 运行 从应用程序的根目录执行此命令 (npm install jsdom) 也会导致 run 失败 (npm WARN enoent ENOENT).按照建议 here I 运行:

npm init
npm install
npm install jsdom

这就是我现在的位置。这是我在 运行 使用 run:

连接应用程序时遇到的错误
> run
[info] Running ca.vgorcinschi.algorithms1_4_34.HotAndColdJS
[error] [stdin]:40
[error]     virtualConsole: jsdom.createVirtualConsole().sendTo(console),
[error]                           ^
[error] 
[error] TypeError: jsdom.createVirtualConsole is not a function
[error]     at [stdin]:40:27
[error]     at [stdin]:61:3
[error]     at ContextifyScript.Script.runInThisContext (vm.js:23:33)
[error]     at Object.runInThisContext (vm.js:95:38)
[error]     at Object.<anonymous> ([stdin]-wrapper:6:22)
[error]     at Module._compile (module.js:571:32)
[error]     at evalScript (bootstrap_node.js:391:27)
[error]     at Socket.<anonymous> (bootstrap_node.js:188:13)
[error]     at emitNone (events.js:91:20)
[error]     at Socket.emit (events.js:188:7)
org.scalajs.jsenv.ExternalJSEnv$NonZeroExitException: Node.js with JSDOM exited with code 1
    at org.scalajs.jsenv.ExternalJSEnv$AbstractExtRunner.waitForVM(ExternalJSEnv.scala:107)
    at org.scalajs.jsenv.ExternalJSEnv$ExtRunner.run(ExternalJSEnv.scala:156)
    at org.scalajs.sbtplugin.ScalaJSPluginInternal$.org$scalajs$sbtplugin$ScalaJSPluginInternal$$jsRun(ScalaJSPluginInternal.scala:697)
    at org.scalajs.sbtplugin.ScalaJSPluginInternal$$anonfun$$anonfun$apply$$anonfun$apply.apply(ScalaJSPluginInternal.scala:814)
    at org.scalajs.sbtplugin.ScalaJSPluginInternal$$anonfun$$anonfun$apply$$anonfun$apply.apply(ScalaJSPluginInternal.scala:808)
    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: 4 s, completed 23-May-2017 9:24:20 PM

如果有人能帮助我,我将不胜感激。

jsdom v10 引入了一些重大变化。 v9,并且 Scala.js <= 0.6.15 没有为这些准备。这就是导致您遇到错误的原因。

升级到 Scala.js 0.6.16 将解决您的问题。它同时支持 jsdom v9 和 v10.