由于 "TypeError: undefined is not a function",sbt 和 scala.js(带有 Node.js)无法使用本地 .js 依赖项运行

标签 sbt scala.js

当我使用 sbt、scala.js(Node.js 上的本地 JavaScript 代码)运行时,我需要有关错误的帮助。

[info] Running net.walend.graph.results.PlotTime
Hello from scala
[error] /Users/dwalend/projects/ScalaGraphMinimizer/toGhPages/target/scala-2.11/toghpages-fastopt.js:1854
[error]   $g["hello"]();
[error]              ^
[error] TypeError: undefined is not a function
[error]     at $c_Lnet_walend_graph_results_PlotTime$.main__V (/Users/dwalend/projects/ScalaGraphMinimizer/toGhPages/target/scala-2.11/toghpages-fastopt.js:1854:14)
[error]     at $c_Lnet_walend_graph_results_PlotTime$.$$js$exported$meth$main__O (/Users/dwalend/projects/ScalaGraphMinimizer/toGhPages/target/scala-2.11/toghpages-fastopt.js:1861:8)
[error]     at $c_Lnet_walend_graph_results_PlotTime$.main (/Users/dwalend/projects/ScalaGraphMinimizer/toGhPages/target/scala-2.11/toghpages-fastopt.js:1864:15)
[error]     at Object.<anonymous> (/Users/dwalend/projects/ScalaGraphMinimizer/toGhPages/target/scala-2.11/toghpages-launcher.js:2:107)
[error]     at Module._compile (module.js:460:26)
[error]     at Object.Module._extensions..js (module.js:478:10)
[error]     at Module.load (module.js:355:32)
[error]     at Function.Module._load (module.js:310:12)
[error]     at Module.require (module.js:365:17)
[error]     at require (module.js:384:17)
org.scalajs.jsenv.ExternalJSEnv$NonZeroExitException: node.js exited with code 1
      at org.scalajs.jsenv.ExternalJSEnv$AbstractExtRunner.waitForVM(ExternalJSEnv.scala:96)
      at org.scalajs.jsenv.ExternalJSEnv$ExtRunner.run(ExternalJSEnv.scala:143)
      at org.scalajs.sbtplugin.ScalaJSPluginInternal$.org$scalajs$sbtplugin$ScalaJSPluginInternal$$jsRun(ScalaJSPluginInternal.scala:479)
      at org.scalajs.sbtplugin.ScalaJSPluginInternal$$anonfun$45$$anonfun$apply$27$$anonfun$apply$28.apply(ScalaJSPluginInternal.scala:539)
      at org.scalajs.sbtplugin.ScalaJSPluginInternal$$anonfun$45$$anonfun$apply$27$$anonfun$apply$28.apply(ScalaJSPluginInternal.scala:533)
      at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)

我最怀疑的是我的 build.sbt。 (它位于一个没有 scala.js 的子项目中。)我认为我有一些不协调的地方,但不知道要尝试哪些其他设置。

scalaVersion := "2.11.7"

scalacOptions ++= Seq("-unchecked", "-deprecation","-feature")

libraryDependencies ++= Seq(
  "org.scala-js" %%% "scalajs-dom" % "0.8.1"
)

//don't need phantomjs . //jsDependencies += RuntimeDOM

jsDependencies += "org.webjars" % "d3js" % "3.5.5-1" / "d3.min.js"

jsDependencies += ProvidedJS / "algorithmTime.js"

scalaJSStage in Global := FastOptStage

persistLauncher := true

我什至无法使用 Node.js 从 algorthmTime.js 中得到“hello”。

function hello() {
  console.log("hello from js")
}

Scala 中的 main() 相当简洁:

对象 PlotTime 扩展 js.JSApp {

def main(): Unit = {
  println("Hello from scala")
  global.hello()

  val png = global.dataToPng("benchmark/results/v0.1.2/dijkstra.csv")
  println(png)
}

}

在尝试 Node.js 之前,我进一步使用 phantom.js 和 Rhino。 sbt run 进入我的本地 javascript 代码并在 d3 内停止

[info] Running net.walend.graph.results.PlotTime
Hello from scala
hello from js
org.mozilla.javascript.EcmaError: TypeError: Cannot call method "querySelector" of undefined (/Users/dwalend/.ivy2/cache/org.webjars/d3js/jars/d3js-3.5.5-1.jar#META-INF/resources/webjars/d3js/3.5.5/d3.min.js#3)
  at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3701)
  at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3679)
  at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3707)
  at org.mozilla.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:3726)
  at org.mozilla.javascript.ScriptRuntime.undefCallError(ScriptRuntime.java:3743)
  at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2269)
  at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2262)
  at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1317)
  at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:815)
  at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:109)
  at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
  at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3102)
  at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:120)
  at org.mozilla.javascript.Context.evaluateString(Context.java:1078)
  at org.scalajs.jsenv.rhino.package$ContextOps$.evaluateFile$extension(package.scala:21)
  at org.scalajs.jsenv.rhino.RhinoJSEnv.org$scalajs$jsenv$rhino$RhinoJSEnv$$internalRunJS(RhinoJSEnv.scala:157)
  at org.scalajs.jsenv.rhino.RhinoJSEnv$Runner.run(RhinoJSEnv.scala:62)
  at org.scalajs.sbtplugin.ScalaJSPluginInternal$.org$scalajs$sbtplugin$ScalaJSPluginInternal$$jsRun(ScalaJSPluginInternal.scala:479)
  at org.scalajs.sbtplugin.ScalaJSPluginInternal$$anonfun$45$$anonfun$apply$27$$anonfun$apply$28.apply(ScalaJSPluginInternal.scala:539)
  at org.scalajs.sbtplugin.ScalaJSPluginInternal$$anonfun$45$$anonfun$apply$27$$anonfun$apply$28.apply(ScalaJSPluginInternal.scala:533)
  at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
[trace] Stack trace suppressed: run last toGhPages/compile:run for the full output.
java.lang.RuntimeException: Exception while running JS code: TypeError: Cannot call method "querySelector" of undefined (/Users/dwalend/.ivy2/cache/org.webjars/d3js/jars/d3js-3.5.5-1.jar#META-INF/resources/webjars/d3js/3.5.5/d3.min.js#3)
  at scala.sys.package$.error(package.scala:27)
  at org.scalajs.jsenv.rhino.RhinoJSEnv.org$scalajs$jsenv$rhino$RhinoJSEnv$$internalRunJS(RhinoJSEnv.scala:173)
  at org.scalajs.jsenv.rhino.RhinoJSEnv$Runner.run(RhinoJSEnv.scala:62)
  at org.scalajs.sbtplugin.ScalaJSPluginInternal$.org$scalajs$sbtplugin$ScalaJSPluginInternal$$jsRun(ScalaJSPluginInternal.scala:479)
  at org.scalajs.sbtplugin.ScalaJSPluginInternal$$anonfun$45$$anonfun$apply$27$$anonfun$apply$28.apply(ScalaJSPluginInternal.scala:539)
  at org.scalajs.sbtplugin.ScalaJSPluginInternal$$anonfun$45$$anonfun$apply$27$$anonfun$apply$28.apply(ScalaJSPluginInternal.scala:533)
  at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)

此错误表明我的代码正在执行它应该执行的操作。然而,互联网智慧说chasing in Rhino "querySelector" is a dead end and Node.js is a better choice

我怀疑系统中缺少一些 sbt 开关,但不知道还要寻找什么。

我也不明白它应该如何工作。我是 javascript 新手,但我不明白这些 javascript 文件中的任何一个如何依赖于任何生成的文件中的任何其他文件。 (scala.js's tutorial 上的示例使用 index.html 页面中的脚本标记将所有内容链接在一起。)

> tree toGhPages/target/scala-2.11/
toGhPages/target/scala-2.11/
├── classes
│   ├── JS_DEPENDENCIES
│   ├── algorithmTime.js
│   └── net
│       └── walend
│           └── graph
│               └── results
│                   ├── PlotTime$.class
│                   ├── PlotTime$.sjsir
│                   └── PlotTime.class
├── toghpages-fastopt.js
├── toghpages-fastopt.js.map
└── toghpages-jsdeps.js

总体情况:我正在尝试使用 sbt、scala.js 和 d3 为 scala 图算法库创建性能图表。第一张图表看起来很有希望,但 github 不支持 README.md 页面上的 javascript。为此,我需要一个简单的图像。我想了解有关 scala.js 和 d3 的更多信息,这吸引了我采用这种方法。

最佳答案

快速修复

为了在 Node.js 中工作,请勿正确声明您希望可见的成员(即没有 var 或命名 function):

hello = function() {
  console.log("hello from js")
};

这是一个可怕的黑客行为,但会解决algorithmTime.js的包含问题。最后给出“正确”的解决方案。

背景

将不同的 JavaScript 文件组合成一个整体是很困难的,因为不存在标准化的方法。传统的 HTML-include 标签仅具有连接所有代码的语义。这是我们尝试在 Scala.js 运行器中模拟的语义。

但是,Node.js 使用 CommonJS模块系统。在该系统中,库显式导出成员,并且使用站点将它们放入命名空间中。这可以避免命名冲突。

示例:

// Library (foo.js)
exports.foo = function() { return 1; };

// Using code
var lib = require("foo.js");
lib.foo() // returns 1

这允许库声明本地值而不会将它们泄漏给调用者。 (注意:虽然我们这里有一个名为 require 的函数,但这不是 RequireJS )。

但是,在 Scala.js 运行程序中,我们希望“仅包含”foo.js,这带来了挑战。我们应该为 require 调用的结果使用什么名称?这就是 commonJSName 的含义(参见下面的示例)。

如果给定依赖项的 commonJSName 未设置,则在 Node.js 运行器中,我们将直接发出

require(<name.js>);

没有将其分配给任何东西。 (为什么不直接转储你所说的文件?再见合理的堆栈跟踪)。

这在 Node.js 中产生了非常有趣的效果。考虑以下文件 (bar.js):

var a = 1;
b = 2;

现在我们做:

require("bar.js")
console.log(a); // undefined
console.log(b); // 2

看起来b 泄漏到全局上下文中,而a 则没有。这就是快速修复起作用的原因。

解决方案

为了获得更好的解决方案,您有两种选择:

  1. 致力于 Node.js,编写特定于其模块系统的库
  2. 自动检测您所在的环境并动态适应(许多 JS 库都这样做)

解决方案1

modules.exports = function() {
  console.log("hello from js")
};

commonJSName 添加到您的依赖项:

jsDependencies += ProvidedJS / "algorithmTime.js" commonJSName "hello"

除了 Node.js 之外,这都会严重失败,原因有两个:

  1. JS VM 可能不支持 CommonJS 样式包含
  2. 像这样覆盖完整的 exports 命名空间不是标准 CommonJS,而是 Node.js (IIRC) 特有的。

解决方案2

自动检测:

var hello = {};

// Scope to prevent leakage
(function(exp) {
  hello.hello = function() {
    console.log("hello from js");
  }
})(exports ? exports : hello);

在这种情况下,您还需要设置commonJSName

此外,您可能已经从代码中怀疑这需要您有一个额外的间接寻址,因为 CommonJS 要求顶级导出是一个对象(IIRC)。因此,您需要调整您的 Scala.js 代码:

global.hello.hello();

但是,如果您的库导出多个符号,那么这可能是个好主意。此外,这可能适用于大多数 JS 环境(并且应该适用于我们通过 Scala.js 提供的三个环境)。

结语

我们(Scala.js 团队)对这种情况非常不满意,因为我们相信包含 JS 库应该像依赖 JVM 领域中的其他 Scala 和/或 Java 库一样简单。然而,我们还没有找到更好的解决方案来解决支持每种包含样式的不足,这需要巨大的设计、工程和维护工作(如果系统发生变化或出现新系统怎么办?) .

相关讨论:#457#706 .

关于由于 "TypeError: undefined is not a function",sbt 和 scala.js(带有 Node.js)无法使用本地 .js 依赖项运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31932340/

相关文章:

scala - 在 Delite 中编译我的第一个 DSL

SBT如何将输入从一个inputTask传递到另一个inputTask

scala - 将所有 SBT 依赖项复制到本地文件夹

scala - 在scala.js中, "Error: Cannot find module ' jsdom'”的可能原因是什么?

scala - 如何让 SBT 并行运行测试套件?

scala - SBT:编译和打包—— "working tree not clean"

scala.js - 如何使用 Scala.js 实现 jQuery 插件

scala.js - 获取 Scala 堆栈跟踪

scala.js - 修复 "You may not export a method of a subclass of js.Any"

Artifact ID 中包含 Scala 和 Scala.js 版本