0

I've been trying to use the FS API in PhantomJS, but I get an error I'm not able to understand when I run the following code

private[scalajssupport] object PhantomFile {
  val fs: PhantomFS = js.Dynamic.global.require("fs").asInstanceOf[PhantomFS]
}

The error I get is:

 TypeError: undefined is not a constructor (evaluating '$g["require"]("fs")')

However, when I run

var fs = global["require"]("fs")

directly in the PhantomJS REPL, it's working fine.

Vaibhav Mule
  • 5,016
  • 4
  • 35
  • 52
ssidorenko
  • 140
  • 5

1 Answers1

1

It turns out that when using PhantomJS to run scala.js code, it is ran in a sandbox with the "webpage" module, which does not have access to require.

The only way to write to the filesystem was to define a callback in onCallback, as seen in the answer to this StackOverflow question.

Community
  • 1
  • 1
ssidorenko
  • 140
  • 5