2

I need to generate uuid in Kotlin/JS and looking to call uuid.v4() from Kotlin/JS. I have gone through the documentation - https://kotlinlang.org/docs/using-packages-from-npm.html and https://kotlinlang.org/docs/js-modules.html but somehow cannot figure out how to invoke.

What I did is this -

  1. Included uuid as an npm dependency in JsMain sourceset
val jsMain by getting {
    dependencies {
        implementation(npm("uuid", "9.0.0"))
    }
}
  1. In JsMain, created a uuid.kt file with this content
@JsModule("uuid")
@JsNonModule
external fun v4(options: Any?, buf: Any?, offset: Any?): String
  1. In Kotlin/Js code, calling v4(null, null, null) doesn't works.

I get this error in console -

Uncaught ReferenceError: v4 is not defined
nightlytrails
  • 2,448
  • 3
  • 22
  • 33

1 Answers1

2

This is how it will work -

  1. declare uuid as an nmp dependency in JsMain sourceSet in build.gradle.kts
val jsMain by getting {
    dependencies {
        implementation(npm("uuid", "9.0.0"))
    }
}
  1. In JsMain, create a uuid.kt file with contents
@JsModule("uuid")
@JsNonModule
external val uuid: dynamic
  1. From Kotlin/Js, use it like
uuid.v4()
nightlytrails
  • 2,448
  • 3
  • 22
  • 33