Home > Software engineering >  Scala Scala.js importing JavaScript module gives error 'method value is not a member of'
Scala Scala.js importing JavaScript module gives error 'method value is not a member of'

Time:04-25

I'm trying to import a JavaScript class module script.js into a scala program main.scala and use its methods add and divide.

I'm using scala.js for importing the JS script and SBT for build.

However when I try running the program, I get the errors:

value add is not a member of example.MyType

value divide is not a member of example.MyType

Could you please help me spot where the problem is?

Thanks in advance!

The code looks like so!

main.scala


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

@js.native
@JSImport("script.js","MyType")
class MyType(var x:Double, var y:Double) extends js.Object {
  add(z: Int) 
  divide(z: Int) 
}

object Hello extends App {
    work() // Sum: 1, Divide: 6

    @JSExport
    def work(): Unit = {
        val added = new MyType(1,2).add(3)
        println(s"Sum: $added,") // 1

        val divided = new MyType(4,3).divide(2)
        println(s"Divide: $divided") // 6
    }
}

script.js:


class MyType {
    constructor(x, y) {

        this.x = x;
        this.y = y;

    }

    add(z){
        let {x,y} = this;
        return x   y   z;
    }

    divide(z){
        let {x,y} = this;
        return (x   y)/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._

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

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

CodePudding user response:

It looks like you got the Scala syntax for defining methods a bit confused. add and divide should be declared as

@js.native
@JSImport("script.js","MyType")
class MyType(var x:Double, var y:Double) extends js.Object {
  def add(z: Int): Double = js.native
  def divide(z: Int): Double = js.native
}
  • Related