# What is the name of my Kotlin-generated JavaScript module?

**URL:** <https://discuss.kotlinlang.org/t/what-is-the-name-of-my-kotlin-generated-javascript-module/17235>\
**Category:** JavaScript\
**Created:** [April 20, 2020, 9:01am UTC](https://discuss.kotlinlang.org/t/what-is-the-name-of-my-kotlin-generated-javascript-module/17235 "2020-04-20T09:01:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cantanima](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cantanima/32/7439_2.png) [@cantanima](https://discuss.kotlinlang.org/u/cantanima)\
**Post date:** [April 20, 2020, 9:01am UTC](https://discuss.kotlinlang.org/t/what-is-the-name-of-my-kotlin-generated-javascript-module/17235/1 "2020-04-20T09:01:38Z")

</div>

I think the JavaScript module is named “untitled2”, because that’s the project name, as well as the name of the .js file produced in `build/js/packages`. However, that doesn’t seem to work, or else I misunderstand how to use it.

**Scenario 1.**

I include the script in the body, and call the function I want from JavaScript’s `main()`, like so:

```
fun main() { apply("myCanvas") }

<canvas id="myCanvas" width="400" height="300"></canvas>
<script src="untitled2.js">
    untitled2.apply("myCanvas")
</script>

```

Everything works fine.

**Scenario 2.**

I include the script in the header:

```
<head> <!-- standard boilerplate omitted -->
<script src="untitled2.js"></script>
</head>

```

When I try to reference this in my HTML file:

```
<canvas id="myCanvas" width="400" height="300"></canvas>
<button type="button" id="button1" onclick="untitled2.apply('myCanvas')">Plot it</button>

```

the JavaScript debugger console reports, `Can't find variable: untitled2`. So apparently that isn’t the module name. Omitting `untitled2` also doesn’t work.

How do I determine the module name – **or** – how do I import it properly? I have read [the usual suspects cited here](https://kotlinlang.org/docs/reference/js-to-kotlin-interop.html).

---

<div class="post-metadata">

**Author:** ![cantanima](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cantanima/32/7439_2.png) [@cantanima](https://discuss.kotlinlang.org/u/cantanima)\
**Post date:** [April 24, 2020, 8:00am UTC](https://discuss.kotlinlang.org/t/what-is-the-name-of-my-kotlin-generated-javascript-module/17235/2 "2020-04-24T08:00:09Z")

</div>

(I hope it’s allowed to bump this topic in the hope that someone notices.)

---

<div class="post-metadata">

**Author:** ![tothiz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tothiz/32/10355_2.png) [@tothiz](https://discuss.kotlinlang.org/u/tothiz)\
**Post date:** [July 31, 2020, 6:45am UTC](https://discuss.kotlinlang.org/t/what-is-the-name-of-my-kotlin-generated-javascript-module/17235/3 "2020-07-31T06:45:46Z")

</div>

Hi,

try window.untitled2.

If you have a special character in your module name like ‘-’ then window[‘untitled-2’].

---

<div class="post-metadata">

**Author:** ![cantanima](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cantanima/32/7439_2.png) [@cantanima](https://discuss.kotlinlang.org/u/cantanima)\
**Post date:** [August 25, 2020, 5:04pm UTC](https://discuss.kotlinlang.org/t/what-is-the-name-of-my-kotlin-generated-javascript-module/17235/4 "2020-08-25T17:04:49Z")

</div>

Thank you for the suggestion. After a couple of days with no reply, I just refactored my code to take a different approach, but the approach I use now is arguably suboptimal. I will try to refactor it again at some point & if I do I will report back.

---

<div class="post-metadata">

**Author:** ![ivan.kubyshkin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ivan.kubyshkin/32/7066_2.png) [@ivan.kubyshkin](https://discuss.kotlinlang.org/u/ivan.kubyshkin)\
**Post date:** [August 26, 2020, 7:02am UTC](https://discuss.kotlinlang.org/t/what-is-the-name-of-my-kotlin-generated-javascript-module/17235/5 "2020-08-26T07:02:12Z")

</div>

Can you attach a link to your project, please?

---

<div class="post-metadata">

**Author:** ![cantanima](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cantanima/32/7439_2.png) [@cantanima](https://discuss.kotlinlang.org/u/cantanima)\
**Post date:** [August 26, 2020, 3:33pm UTC](https://discuss.kotlinlang.org/t/what-is-the-name-of-my-kotlin-generated-javascript-module/17235/6 "2020-08-26T15:33:50Z")

</div>

I don’t have it on GitHub, and I don’t believe I have a copy of the older code. Do you want a link to a webpage that shows the code in action? (It will also so how I execute it in JavaScript.)

---

<div class="post-metadata">

**Author:** ![ivan.kubyshkin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ivan.kubyshkin/32/7066_2.png) [@ivan.kubyshkin](https://discuss.kotlinlang.org/u/ivan.kubyshkin)\
**Post date:** [September 14, 2020, 6:55am UTC](https://discuss.kotlinlang.org/t/what-is-the-name-of-my-kotlin-generated-javascript-module/17235/7 "2020-09-14T06:55:33Z")

</div>

I don’t think this link will help. I failed to reproduce the problem, and it’s hard to help you without a sample project.

---

<div class="post-metadata">

**Author:** ![cantanima](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cantanima/32/7439_2.png) [@cantanima](https://discuss.kotlinlang.org/u/cantanima)\
**Post date:** [December 15, 2020, 2:09am UTC](https://discuss.kotlinlang.org/t/what-is-the-name-of-my-kotlin-generated-javascript-module/17235/8 "2020-12-15T02:09:03Z")

</div>

Sorry it’s taken so long to get back to you. I only had the time to rework my code today, to see if I could make it work. The good news is that I believe I have figured out the problem.

When I tried analyzing the module via Firefox’s Web Console I noticed the name mangling. I added a `@JSName` annotation, and it worked beautifully. Previously I was mainly using Safari’s Web Console, but even today Safari’s Web Console was not showing me anything from my module, and sometimes not even the module name! It seems to be quite buggy.

A couple of Caveats:

- I am not entirely sure this was the problem. As part of my attempts to get this working, I created a new project today and copied the relevant code, but I noticed that Idea set up the new project differently from the old one. So, there may have been something else at work.

- [This page](https://kotlinlang.org/docs/reference/js-to-kotlin-interop.html) states that names may be mangled, but the example and the wording strongly imply that this is rare: “In some cases (for example, to support overloads).” Nearly all of my function names are mangled, and none of them are class members; indeed, none of them clash with anything else! I don’t think the automatically-generated program’s functions were mangled, so perhaps it kicks in only at a certain size, but as far as I can tell, the only function in my code whose name was _not_ mangled is `main()`. If true, I humbly suggest that the documentation be reworded. (If this means I should fill out a ticket, tell me where to go.)

---

<div class="post-metadata">

**Author:** ![cantanima](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cantanima/32/7439_2.png) [@cantanima](https://discuss.kotlinlang.org/u/cantanima)\
**Post date:** [December 16, 2020, 3:04am UTC](https://discuss.kotlinlang.org/t/what-is-the-name-of-my-kotlin-generated-javascript-module/17235/9 "2020-12-16T03:04:36Z")

</div>

I have encountered and a related problem, which may well have been the same thing. When I tried to produce a `BrowserProductionRun`, the resulting JavaScript files lacked any references to anything but the `main()` function.

I am writing a JavaScript library that I want to call from an HTML file, so I have a number of functions that aren’t called within the module itself, but will be called from JavaScript. Unfortunately, Kotlin’s Dead Code Elimination was eliminating those functions. As per [this page](https://kotlinlang.org/docs/reference/javascript-dce.html), simply adding

```
dceTask {
   keep("myModule.myFunction")
}

```

to `build.gradle.kts` did the trick.

Is this the only way to prevent the compiler from eliminating functions it thinks are pointless?
