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

**URL:** <https://discuss.kotlinlang.org/t/kotlin-js-how-to-rely-on-npm-dependencies-and-how-to-do-it-in-a-multiplatform-project/11246>\
**Category:** JavaScript\
**Created:** [January 20, 2019, 6:21pm UTC](https://discuss.kotlinlang.org/t/kotlin-js-how-to-rely-on-npm-dependencies-and-how-to-do-it-in-a-multiplatform-project/11246 "2019-01-20T18:21:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [January 20, 2019, 6:21pm UTC](https://discuss.kotlinlang.org/t/kotlin-js-how-to-rely-on-npm-dependencies-and-how-to-do-it-in-a-multiplatform-project/11246/1 "2019-01-20T18:21:21Z")

</div>

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](https://github.com/srs/gradle-node-plugin) 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?

---

<div class="post-metadata">

**Author:** ![seekdasky](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/seekdasky/32/4673_2.png) [@seekdasky](https://discuss.kotlinlang.org/u/seekdasky)\
**Post date:** [January 23, 2019, 7:34pm UTC](https://discuss.kotlinlang.org/t/kotlin-js-how-to-rely-on-npm-dependencies-and-how-to-do-it-in-a-multiplatform-project/11246/2 "2019-01-23T19:34:04Z")

</div>

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 🙂

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

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [January 24, 2019, 8:00am UTC](https://discuss.kotlinlang.org/t/kotlin-js-how-to-rely-on-npm-dependencies-and-how-to-do-it-in-a-multiplatform-project/11246/3 "2019-01-24T08:00:22Z")

</div>

Thanks 🙂 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?

---

<div class="post-metadata">

**Author:** ![seekdasky](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/seekdasky/32/4673_2.png) [@seekdasky](https://discuss.kotlinlang.org/u/seekdasky)\
**Post date:** [January 24, 2019, 2:25pm UTC](https://discuss.kotlinlang.org/t/kotlin-js-how-to-rely-on-npm-dependencies-and-how-to-do-it-in-a-multiplatform-project/11246/4 "2019-01-24T14:25:21Z")

</div>

Sure thing, you can check my [gradle file](https://github.com/SeekDaSky/Sok/blob/master/gradle/publish-npm.gradle).

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 🙂

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [October 16, 2019, 10:59pm UTC](https://discuss.kotlinlang.org/t/kotlin-js-how-to-rely-on-npm-dependencies-and-how-to-do-it-in-a-multiplatform-project/11246/5 "2019-10-16T22:59:46Z")

</div>

Late reply, but back on topic 😛  
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!

---

<div class="post-metadata">

**Author:** ![seekdasky](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/seekdasky/32/4673_2.png) [@seekdasky](https://discuss.kotlinlang.org/u/seekdasky)\
**Post date:** [October 17, 2019, 6:58am UTC](https://discuss.kotlinlang.org/t/kotlin-js-how-to-rely-on-npm-dependencies-and-how-to-do-it-in-a-multiplatform-project/11246/6 "2019-10-17T06:58:28Z")

</div>

> [@lamba92](#):
>
> 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?

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.

> [@lamba92](#):
>
> In the meantime the Kotlin/JS evolved so much and allows to create a `package.json` ,

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.

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [October 17, 2019, 11:13am UTC](https://discuss.kotlinlang.org/t/kotlin-js-how-to-rely-on-npm-dependencies-and-how-to-do-it-in-a-multiplatform-project/11246/7 "2019-10-17T11:13:04Z")

</div>

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?
