3

This question appears fundamentally the same as Linker error in ScalaJS: "Referring to non-existent class", but seems to differ in specifics. I don't specify a JVM version in build.sbt.

I'm trying to access a file in my scalajs app using scala.io.Source, or java.io.File. As soon as I attempt to use either I get a link fail:

...
[info] Fast optimizing C:\Users\Tim\Documents\GitHub\binpack\target\scala-2.11\binpack-fastopt.js
[error] Referring to non-existent class java.io.File
[error]   called from client.jsClient$.onFileChosen(japgolly.scalajs.react.SyntheticEvent)scala.Function0
...

Is there a method by which I can diagnose what's going on here? I'm assuming the problem is not specific to java.io.File, but don't know what the next step would be.

This is scala 2.11.8, with sbt 0.13.7.

Community
  • 1
  • 1
Tim Barrass
  • 4,813
  • 2
  • 29
  • 55

1 Answers1

4

java.io.File is not portable to Scala.js because it's specifically tied to the JVM. See http://www.lihaoyi.com/hands-on-scala-js/#PortingJavaAPIs (just scroll to the very bottom) for a little more detail.

So, if you want to do filesystem IO in Scala.js, you'll need to use a solution specifically implemented for JavaScript, like io.js. Here's an example: http://bchazalet.github.io/2015/07/20/scalajs-electron-skeleton-part-2/

Yawar
  • 11,272
  • 4
  • 48
  • 80
  • Thanks for the explanation. Looks like there are other ways to read files too that don't need to use node.js? http://stackoverflow.com/q/30332326/389828 – Tim Barrass Oct 21 '16 at 07:08
  • 1
    @TimBarrass the `FileReader` API can read a file if it is selected by the user in an `` element, right. But it can't read arbitrary files on the user's computer. – Yawar Oct 19 '20 at 14:55