Search code examples
node.jsscalareactjsscala.jsscalajs-react

Unable to run scalajs-react project


I'm just trying to set up scalajs-react using official docs, but faced an issue when I tried to run this sbt run:

[info] Fast optimizing C:\temp\scalajs-react\target\scala-2.12\scala-js-tutorial-fastopt.js
[info] Running org.example.TutorialApp
[error] module.js:471
[error]     throw err;
[error]     ^
[error] 
[error] Error: Cannot find module 'react'
[error]     at Function.Module._resolveFilename (module.js:469:15)
[error]     at Function.Module._load (module.js:417:25)
[error]     at Module.require (module.js:497:17)
[error]     at require (internal/module.js:20:19)
[error]     at C:\Users\avkar\AppData\Local\Temp\1484704765073-0\react-dom.js:16:24
[error]     at Object.<anonymous> (C:\Users\avkar\AppData\Local\Temp\1484704765073-0\react-dom.js:40:3)
[error]     at Module._compile (module.js:570:32)
[error]     at Object.Module._extensions..js (module.js:579:10)
[error]     at Module.load (module.js:487:32)
[error]     at tryModuleLoad (module.js:446:12)
org.scalajs.jsenv.ExternalJSEnv$NonZeroExitException: Node.js 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:684)
    at org.scalajs.sbtplugin.ScalaJSPluginInternal$$anonfun$60$$anonfun$apply$43$$anonfun$apply$44.apply(ScalaJSPluginInternal.scala:771)
    at org.scalajs.sbtplugin.ScalaJSPluginInternal$$anonfun$60$$anonfun$apply$43$$anonfun$apply$44.apply(ScalaJSPluginInternal.scala:765)
    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 exited with code 1
[error] Total time: 3 s, completed 18.01.2017 8:59:25

I have Node.js on my machine, but there was no 'react' module. So, I decided to install those modules from build.sbt using the command npm install -g [email protected] [email protected]

After that, I have the following output for npm list -g --depth=0:

+-- [email protected]
+-- [email protected]
`-- [email protected]

However, it changes nothing. Still the same error output for sbt run

Does anybody know what's this?


Solution

  • You don't need to install anything into node.js directly. npm install -g react... will have no effect. If you want to run your app using sbt run then it's going to be SBT settings via which you install React.

    The scalajs-react docs show how to install React via jsDependencies in SBT. You should still read through the Scala.JS tutorial to find other required settings. I don't use sbt run myself but according to the tutorial you'll probably need to add jsDependencies += RuntimeDOM, run npm install jsdom, and maybe even use skip in packageJSDependencies := false.

    If that doesn't work, this is a Scala.JS usage issue, not specific to scalajs-react.