'Scala scala.js cannot find module js script in sbt build

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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source