I am stuck on trying to import an npm library call...
# javascript
r
I am stuck on trying to import an npm library called whereby (an embeddable meeting)
I have:
Copy code
@JsModule("@whereby.com/browser-sdk/embed")
@JsNonModule
external object Whereby {}
When I try to run this, I get: Module not found: Error: Package path ./dist/embed/index.esm.js is not exported from package /expanse/code/startup/interview/build/js/node_modules/@whereby.com/browser-sdk (see exports field in /expanse/code/startup/interview/build/js/node_modules/@whereby.com/browser-sdk/package.json) Their package.json (in @whereby.com/browser-sdk) is attached.
package.json
Ah - the above is with whereby.js in webpack.conf.d with:
Copy code
config.resolve = config.resolve || {};
config.resolve.alias = config.resolve.alias || {};
config.resolve.alias['@whereby.com/browser-sdk/embed'] = '@whereby.com/browser-sdk/dist/embed/index.esm.js';
If I try the above without this file in place, I get this error instead: Module not found: Error: Can't resolve '@whereby.com/browser-sdk/embed' in '/expanse/code/startup/interview/build/js/packages/interview/kotlin'
folder structure in node_modules:
Here is another experiment:
Copy code
@JsModule("@whereby.com/browser-sdk/dist/embed/index.esm.js")
@JsNonModule
external object Whereby

fun initWhereby() {
    console.dir(Whereby)
Module not found: Error: Package path ./dist/embed/index.esm.js is not exported from package /expanse/code/startup/interview/build/js/node_modules/@whereby.com/browser-sdk (see exports field in /expanse/code/startup/interview/build/js/node_modules/@whereby.com/browser-sdk/package.json) ...which is baffling given:
Copy code
"exports": {
    "./react": {
      "import": "./dist/react/index.esm.js",
      "types": "./dist/react/index.d.ts",
      "require": "./dist/react/index.cjs",
      "default": "./dist/react/index.esm.js"
    },
    "./embed": {
      "import": "./dist/embed/index.esm.js",
      "types": "./dist/embed/index.d.ts",
      "require": "./dist/embed/index.cjs",
      "default": "./dist/embed/index.esm.js"
    }
  },
t
Copy code
@file:JsModule("@whereby.com/browser-sdk/embed")

external fun someFunction {}
+
target = "es2015"
for Kotlin?
r
Hm. If I try:
Copy code
@file:JsModule("@whereby.com/browser-sdk/embed")
package rcp.libs

external fun render()
...and
Copy code
object WherebyHelper {

    fun initWhereby() {
        ...
        if (wherebyMeetings.length > 0) {
            render()
        }
    }
}
...I get: e: file///expanse/code/startup/interview/src/jsMain/kotlin/rcp/libs/WherebyHelper.kt21:13 When accessing module declarations from UMD, they must be marked by both @JsModule and @JsNonModule
t
+
target = "es2015"
for Kotlin?
r
Where does that go? I had:
Copy code
webpackTask {
          output.libraryTarget = "es2015"
       }
t
r
Hm, I can't make the IDE happy with any variation of ☝️ , but I tried:
Copy code
useEsModules()
...and at least I'm able to start the app. There are strange errors which I'll debug. And ok, now I understand. So in general the presence of this kind of
exports
declaration in package.json implies the need for a file: based mapping? (i.e. https://kotlinlang.org/docs/js-modules.html#apply-jsmodule-to-packages)
t
Kotlin 2+?
r
No, not yet. I am waiting for the .1 release 🙂
t
Also:
Copy code
moduleKind = "es"
It's for Kotlin 1.9