# Kotlin/JS 1.4, how to output a NPM package?

**URL:** <https://discuss.kotlinlang.org/t/kotlin-js-1-4-how-to-output-a-npm-package/19128>\
**Category:** JavaScript\
**Created:** [September 11, 2020, 5:43pm UTC](https://discuss.kotlinlang.org/t/kotlin-js-1-4-how-to-output-a-npm-package/19128 "2020-09-11T17:43:04Z")\
**Posts on this page:** 5\
**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:** [September 11, 2020, 5:43pm UTC](https://discuss.kotlinlang.org/t/kotlin-js-1-4-how-to-output-a-npm-package/19128/1 "2020-09-11T17:43:05Z")

</div>

Say you have a simple project with just the Kotlin standard library and one dependency from NPM. What would be a good pattern to output an NPM package? Also which tasks of the Kotlin/JS plugin (or Kotlin/MPP with `js()`) are key to do so?

---

<div class="post-metadata">

**Author:** ![ilya.goncharov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.goncharov/32/6969_2.png) [@ilya.goncharov](https://discuss.kotlinlang.org/u/ilya.goncharov)\
**Post date:** [September 22, 2020, 3:33pm UTC](https://discuss.kotlinlang.org/t/kotlin-js-1-4-how-to-output-a-npm-package/19128/2 "2020-09-22T15:33:37Z")

</div>

There are no such tasks in Gradle plugin yet, but we plan to add it in future. You can setup it on your own. We can task `:publicPackageJson` for generate `package.json` which can be used for publishing into NPM registry.

If you use `legacy` compiler, you have 2 options. You can publish compilation output js file (`:compileKotlinJs`). In this case you need to add all Kotlin/JS modules as dependencies to your `package.json`. But in fact there can be situation when some of your dependencies will not be in NPM registry. In this case you get error on `npm install` if your package.  
So there is alternative variant, you can use webpacked version of your module. In this case you get one big bundle js file which can be used as a library. But in this case you get all NPM dependencies bundled as well, you can use configuration via `webpack.config.d` and [Webpack externals](https://webpack.js.org/configuration/externals/)

If you use IR compiler, you can use output of compilation, because it compile one javascript file from all Kotlin/JS modules, and using `:publicPackageJson` you can get valid `package.json` with all external npm dependencies

---

<div class="post-metadata">

**Author:** ![chrisjowen.1](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/chrisjowen.1/32/8372_2.png) [@chrisjowen.1](https://discuss.kotlinlang.org/u/chrisjowen.1)\
**Post date:** [March 10, 2021, 1:51pm UTC](https://discuss.kotlinlang.org/t/kotlin-js-1-4-how-to-output-a-npm-package/19128/3 "2021-03-10T13:51:04Z")

</div>

For **not** IR compilation I am finding that:

Option 1 - Publishing to npm

As mentiopned, the fact some dependencies wont be in NPM. Found this with some kotlin mutliplatform libs were only pushed to maven etc and not published to NPM.

But honestly I can live with this as any missing deps we just publish to our nexus.

However, even this way isnt an option as the package.json generated has absolute paths to the `packages_imported` folder dependencies instead of relative paths.

Option 2 - The one big bundled js in dist (or after manually running the webpack build)

Inspecting the output of this shows that there are a lot of things missing. We are creating a library code, with paths that aren’t called inside the codebase, but they are intended to be exported and called from the library consumer.

Seems the DCE is seeing paths that arent used and removing them from the output. But as mentioned they need to be there, and also need to not have obfuscated names when minified.

I tried the gradle kotlin plugins `js/browser/keep` settings to try and hint to DCE to leave this alone but the codepaths were always still missing.

---

<div class="post-metadata">

**Author:** ![Lamba92\_v2](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92_v2/32/8387_2.png) [@Lamba92\_v2](https://discuss.kotlinlang.org/u/Lamba92_v2)\
**Post date:** [March 10, 2021, 10:34pm UTC](https://discuss.kotlinlang.org/t/kotlin-js-1-4-how-to-output-a-npm-package/19128/4 "2021-03-10T22:34:00Z")

</div>

Now with the IR compiler you can, if you enable `binaries.executable()`, a task named `compileProductionExecutableKotlinJs` with type `KotlinJsIrLink` will be added.

This task generates a JS bundled output with all dependencies from Maven but not from NPM. You could use that one with the `node_modules` folder created by the plugin to webpack all together. I’ve done a similar thing but for NodeJs [here](https://github.com/lamba92/kotlingram/blob/master/examples/js-bot/build.gradle.kts#L64-L145).

---

<div class="post-metadata">

**Author:** ![alfredlopez](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alfredlopez/32/8112_2.png) [@alfredlopez](https://discuss.kotlinlang.org/u/alfredlopez)\
**Post date:** [September 16, 2021, 2:37pm UTC](https://discuss.kotlinlang.org/t/kotlin-js-1-4-how-to-output-a-npm-package/19128/5 "2021-09-16T14:37:50Z")

</div>

Hello Lamberto,

Not sure how to ask this, but I have a MPP that currently only has a JS target. The link that you provided, is that all I need to put in my project? Or is it dependent on your Kotlingram project?

Thanks!
