Hi there! We are currently trying to use Kotlin m...
# javascript
s
Hi there! We are currently trying to use Kotlin multiplatform to generate client libraries for JVM and the Node.js ecosystem. There, we got an issue with our generated commonjs artifacts for the node.js target. Our goal is to publish the generated nodejs client library in a NPM registry, but we run into troubles there. I'm not sure if it's a problem of my understanding, a configuration issue or we're trying to do something that is not (yet) really supported. Please bear with me that I try to write my understanding and my situation in somewhat more detail. • The generated client library at ./kotlin/build/js/packages/<project-name> has a package.json file with dependencies that can not be found on npm.js org. For this reason, it doesn't work standalone if I want to publish my generated client lib itself as a NPM package. • If you try to npm install the generated artifact, it will therefore look for packages like https://www.npmjs.com/package/ktor-ktor-client-js which are not published (..yet?) • There are effective dependencies, which are not specified in the package.json of either the generated library file or the packages_imported npm project. E.g. "node-fetch" is a dependency of ktor-ktor-client-core, but nowhere defined as a package.json dependency • The actual missing modules are found in ../packages_imported. • The actual project at build/js itself is a yarn multi-workspace project which now takes those packages_imported. This works if you intend to run the node.js project as a standalone project because of yarn features (dependency resolution). • My open Question is: Is is currently possible to get a standalone working, npm publish compatible
js("node").compilations["main"]
artifact? Thank you for your time and effort you're putting into this! The relevant part of our build.gradle.kts
Copy code
js("node") {
        nodejs {

        }
        compilations.all {
            compileKotlinTask.kotlinOptions {
                metaInfo              = true
                sourceMap             = true
                moduleKind            = "commonjs"
                sourceMapEmbedSources = null
            }
        }
    }
[...]
        js("node").compilations["main"].defaultSourceSet {
            dependencies {
                implementation(kotlin("stdlib-js"))
                implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$coroutinesVersion")

                implementation("io.ktor:ktor-client-js:$ktorVersion")
                implementation("io.ktor:ktor-client-serialization-js:$ktorVersion")
                implementation("io.ktor:ktor-client-json-js:$ktorVersion")
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serializationVersion")
                implementation("io.ktor:ktor-client-auth-js:$ktorVersion")

                implementation(npm("uuid", "3.3.3"))
            }
        }
👍 1
e
I'm trying to do this myself, and from what I can tell, this article is still mostly reflect the current situation (version 1.3.61): https://medium.com/swlh/kotlin-to-jar-and-npm-87a5b0ca2dff You don't have to explicitly import kotlin anymore, but you still need some glue code to "unwrap" your package name from the generated module, and a
package.json
that points at your glue code, both located in
src/jsMain/resources
. Once that's done, you can just unzip
build/libs/somepackage-js-someversion.jar
in your build pipeline, fetch the actual version with
gradle properties | grep ^version | cut -d" " -f2
and set that with ``npm --no-git-tag-version someversion` and publish to your npm registry of choice, as you normally would.
The generated code in that jar-file seems to be a self-contained node module though, so you don't actually need the glue code if you're fine with your consumers having to "unwrap" your package, eg.
Copy code
import { com } from 'your-module'
const what_i_actually_want = com.example.your-module;
s
Thanks for the reply! The article looks really helpful, but it's also solidifying my hunch, that Kotlin 2 JS is not there yet - at least for Node.js / commonjs as a target. The nodejs code in the .jar file does miss a package.json in my build, which makes it not self-contained. Did I miss something?
👍 1
e
The nodejs code in the .jar file does miss a package.json in my build, which makes it not self-contained. Did I miss something?
No, you're right about that. I thought it did, but it doesn't, so you actually have to add that yourself either way.
s
Ok, thanks for your replies @Eivind Nilsbakken ! They were really helpful!