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: