Search code examples
scala.jsscalajs-bundler

Cannot find node module fs when bundling module via scalajs-bundler


I'm trying to connect a scala.js app to a node module. I have not done this before.

On fastOptJS::webpack I am getting a build failure with:

target/scala-2.12/scalajs-bundler/main/node_modules/fs doesn't exist
target/scala-2.12/scalajs-bundler/main/node_modules/fs.webpack.js doesn't exist
target/scala-2.12/scalajs-bundler/main/node_modules/fs.web.js doesn't exist
target/scala-2.12/scalajs-bundler/main/node_modules/fs.js doesn't exist
target/scala-2.12/scalajs-bundler/main/node_modules/fs.json doesn't exist

build.sbt

enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin)

name := "Toon Brew"

scalaVersion := "2.12.2"

scalaJSUseMainModuleInitializer := true

libraryDependencies += "be.doeraene" %%% "scalajs-jquery" % "0.9.1"

skip in packageJSDependencies := false

jsDependencies += "org.webjars" % "jquery" % "2.1.4" / "2.1.4/jquery.js"

npmDependencies in Compile += "fantasy-names" -> "1.1.2"

plugins.sbt

addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.17")

addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.6.0")

facade

package toonbrew

import scala.scalajs.js
import scala.scalajs.js.annotation.JSImport

@JSImport("fantasy-names", JSImport.Namespace)
@js.native
object FantasyNames extends js.Object {
  def names(cat: String, subCat: String, names: Int, gender: Int): js.Array[String] = js.native
}

The entire repo

How can I get around this error?


Solution

  • Ignore fs by extending the scalajs-bundler default webpack config.

    no-fs.webpack.config.js

    module.exports = require('./scalajs.webpack.config');
    
    module.exports.node = {fs: 'empty'};
    

    build.sbt

    webpackConfigFile := Some(baseDirectory.value / "no-fs.webpack.config.js")
    

    With credit to @julien-richard-foy