Kotlin/JS, how to rely on npm dependencies and how to do it in a multiplatform project

I need to use an npm dependency in my multiplatform project. Now if I was using the old mpp model I could have applied the kotlin-frontend-plugin to the JS module but with the new model I can’t.
I could use this Node plugin for Gradle wich is fine, but how do I pack the library to publish on maven? I mean should I add the dependencies as well or just let the user of the lib do it?
So in summary:

  • is it possible to use the kotlin-fronted-plugin inside the new mpp model?
    • if yes, should I pack the dependencies with my library?
      • if yes, how?
      • if no, why?
    • if no, should I pack the dependencies with the Node Plugin?
      • if yes, how?
      • if no, why?

I had the same problem, my approach was to let NPM do what it does, manage dependencies and not bundle anything with my library when publishing. As people need to set up Node/NPM to use your library anyway I think it is not a problem.

What I also do is publish the Kotlin/JS version of my library on the NPM registry so I can put in the package.json the peerDependencies I need. With all that, when somebody uses the Kotlin/JS version of the lib, it uses the code published on Maven for compilation and npm install it for execution. With that I don’t pollute what I publish on Maven and it still is “easy” to use.

I don’t know if this is the right way to do it and it is mainly inspired by what the kotlinx-coroutine project does.

I hope this helps you make your decision :slight_smile:

EDIT: I only use the gradle-node-plugin, I never tried with kotlin-frontend-plugin

Thanks :slight_smile: It makes sense tho, I think I’ll roll with that too.
Could you show me how you managed to setup all the stuff for publishing on npm?

Sure thing, you can check my gradle file.

Small explanation, you need a new directory in which you will have your package.json file, mine have variables in it to update dependencies by updating the gradle.properties file so I expand it.Copy sourceSets.main.output in you NPM directory (the compiled code). When all of this is done you can execute npm publish (the task is named publishNpm for me).

Again all credits go to the maintainers of the kotlinx-coroutines I basically copied and adapted the code a bit :slight_smile:

1 Like

Late reply, but back on topic :stuck_out_tongue:
What if you have a js dependency that is on Maven but not on NPM and you want to let your library be used from a pure js project?

Should you manually unpack all the JS Maven only dependencies and put them into your output? Kinda cumbersome atm.

In the meantime the Kotlin/JS evolved so much and allows to create a package.json, but the dependencies that comes from maven will have a fake entry in the package.json since they may not have the same name on NPM or not being on NPM at all!

I would bundle them myself, but it is indeed not ideal. The cleanest solution would some sort of plugin for NPM that would allow it to fetch dependencies on maven in addition to the NPM registry.

I must say I haven’t played with MPP those recent month so I didn’t check the new features (especially the automatic testing with K/JS that could replace my hacky setup). But JetBrains is on the right path.

1 Like

May I ask you how to bundle them inside a single node module? Like the scaffholding and the package.json.

Consider that now the Kotlin/JS plugin expands all the Maven JS dependency into a node_module folder along with the actual node moduels that are on NPM. I guess I can copy those unzipped modules somehow or iterate on the maven dependencies and extract them by myself. Still, don’t know how to pack them together.

Also, how to get the reference for the Maven Jars into a MPP project?