scala-js/scala-js-dom

Window not Defined

Closed this issue · 1 comments

FA5I commented

Hi,

I have installed Node and jsdom using npm.

Now when I try and run this program I get the following error:

My js main looks like this:

package example

object Main {
  def main(args: Array[String]): Unit = {
    val lib = new MyLibrary
    println(lib.sq(2))

    import org.scalajs.dom
    dom.window.alert("Hi from Scala-js-dom")

    println(s"Using Scala.js version ${System.getProperty("java.vm.version")}")
  }
}

My build.sbt:

ThisBuild / scalaVersion := "2.13.2"

lazy val root = project.in(file(".")).
  aggregate(foo.js, foo.jvm).
  settings(
    publish := {},
    publishLocal := {},
  )

lazy val foo = crossProject(JSPlatform, JVMPlatform).in(file(".")).
  settings(
    name := "jstest",
    version := "0.1-SNAPSHOT",
    libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "1.0.0"
  ).
  jvmSettings(
    // Add JVM-specific settings here
  ).
  jsSettings(
    // Add JS-specific settings here
    scalaJSUseMainModuleInitializer := true,

  )

jsEnv := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv()

My plugins.sbt:

addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.0.0")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.0.0")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.18.0")

libraryDependencies += "org.scala-js" %% "scalajs-env-jsdom-nodejs" % "1.0.0"

However when I run fooJS/run in the sbt terminal, I get the following:

/Users/fa/Desktop/jstest/js/target/scala-2.13/jstest-fastopt.js:973
    $thiz.Lorg_scalajs_dom_package$__f_window = window;
                                                ^
ReferenceError: window is not defined
    at $p_Lorg_scalajs_dom_package$__window$lzycompute__Lorg_scalajs_dom_raw_Window (/Users/fa/Desktop/jstest/js/target/scala-2.13/jstest-fastopt.js:973:49)
    at $c_Lorg_scalajs_dom_package$.window__Lorg_scalajs_dom_raw_Window (/Users/fa/Desktop/jstest/js/target/scala-2.13/jstest-fastopt.js:1012:79)
    at $c_Lexample_Main$.main__AT__V (/Users/fa/Desktop/jstest/js/target/scala-2.13/jstest-fastopt.js:735:36)
    at $s_Lexample_Main__main__AT__V (/Users/fa/Desktop/jstest/js/target/scala-2.13/jstest-fastopt.js:726:23)
    at /Users/fa/Desktop/jstest/js/target/scala-2.13/jstest-fastopt.js:2022:1
    at /Users/fa/Desktop/jstest/js/target/scala-2.13/jstest-fastopt.js:2023:4
    at Script.runInThisContext (vm.js:120:20)
    at Object.runInThisContext (vm.js:311:38)
    at [stdin]:8:25
    at Script.runInThisContext (vm.js:120:20)
[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 fooJS / Compile / run for the full output
[error] (fooJS / Compile / run) org.scalajs.jsenv.ExternalJSRun$NonZeroExitException: exited with code 1
[error] Total time: 8 s, completed 24 Jun 2020, 22:46:39

If i remove the jsdom specific code, everything seems to run fine. Why is this occuring and is there a quick fix?

sjrd commented

Hello,

This looks like a question on using Scala.js, rather than a bug report. The GitHub Issues in this repo are for bug reports and feature requests only. Please ask questions on StackOverflow or on Gitter, where more than just the core developers can see and answer.

Thank you for your understanding.