0

I'm trying to import a JS Script script.js into a scala program App.scala and use its methods add and divide.

However on running the program, I keep getting the error, Cannot find module 'resources/script.js'.

The sbt main folder structure looks something like below:

../main

  /resources

  -script.js

  /scala/example

  -App.scala

Could you please help me spot the problem?

Thanks in advance!

App.scala:


package example
import scala.scalajs.js
import scala.scalajs.js.annotation._

@js.native
@JSImport("resources/script.js","MyType")
class MyType(var x:Double, var y:Double) extends js.Object {
    ...
}

object Hello extends App {
    work() 

    @JSExport
    def work(): Unit = {
    ...
    }
}

script.js:

class MyType {
    constructor(x, y) {
      ...
    }

    add(z){
      ...
    }

    divide(z){
       ...
    }
};

module.exports = {MyType};

plugins.sbt:


addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.10.0")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.0.0")

build.sbt:


import Dependencies._
import org.scalajs.linker.interface.ModuleInitializer

ThisBuild / scalaVersion     := "2.13.8"
ThisBuild / version          := "0.1.0-SNAPSHOT"
ThisBuild / organization     := "com.example"
ThisBuild / organizationName := "example"

lazy val root = (project in file("."))
  .settings(
    name := "add",
    libraryDependencies += scalaTest % Test
  )

enablePlugins(ScalaJSPlugin)
scalaJSUseMainModuleInitializer := true

// ECMAScript
scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.ESModule) }
// CommonJS
scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.CommonJSModule) }

// See https://www.scala-sbt.org/1.x/docs/Using-Sonatype.html for instructions on how to publish to Sonatype.

2
  • "However on running the program" How do you run the program? From sbt? From a web page? Commented Apr 26, 2022 at 8:57
  • sbt run (command terminal) Commented Apr 26, 2022 at 9:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.