# How to share code between multiple multiplatform modules used in JS?

**URL:** <https://discuss.kotlinlang.org/t/how-to-share-code-between-multiple-multiplatform-modules-used-in-js/24083>\
**Category:** Support\
**Created:** [January 31, 2022, 12:51pm UTC](https://discuss.kotlinlang.org/t/how-to-share-code-between-multiple-multiplatform-modules-used-in-js/24083 "2022-01-31T12:51:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwert](https://avatars.discourse-cdn.com/v4/letter/q/7bcc69/32.png) [@qwert](https://discuss.kotlinlang.org/u/qwert)\
**Post date:** [January 31, 2022, 12:51pm UTC](https://discuss.kotlinlang.org/t/how-to-share-code-between-multiple-multiplatform-modules-used-in-js/24083/1 "2022-01-31T12:51:21Z")

</div>

I’m currently trying out kotlin multiplatform with jvm and js targets. The idea was to create a module structure like this:

- a-client: Multiplatform module containing model code and methods needed to call backend Api A
- b-client: Multiplatform module containing model code and methods needed to call backend Api B
- common-client: Multiplatform module containing code that is shared by a-client and b-client (for example creation of the ktor-HttpClient)
- frontend: react (typescript) module using a-client/b-client/common-client

Here is a full example of this: [GitHub - Fabian-G/shared-mpp](https://github.com/Fabian-G/shared-mpp)

Like I said I am using ktor-client to make the HttpRequests in a-client and b-client. The client itself is created in common-client and then injected via constructor parameter to a-client/b-client.  
I do it like that, because I would prefer if I could manage the HttpClient in the frontend module so that I could hand the very same HttpClient to a-client and b-client if I wanted to.  
This does not work though. As soon as either a-client or b-client tries to use it I get this error at runtime:

```auto
Uncaught (in promise) TypeError: this. __this__ 10._client.execute_ixyi6b_k$ is not a function
    doResume_0_k$ HttpStatement.kt:104
    executeUnsafe_0_k$ HttpStatement.kt:101
    doResume_0_k$ TodosApi.kt:17
    findById_ha5a7z_k$ TodosApi.kt:13
    doResume_0_k$ JsTodosApi.kt:18
    invoke_mzru1z_k$ JsTodosApi.kt:17
    l bundle.js:38410
    doResume_2_0_k$ IntrinsicsJs.kt:163
    doResume_0_k$ bundle.js:11608
    resumeWith_jccoe6_k$ Standard.kt:55
    resumeWith_bnunh2_k$ bundle.js:11512
    resumeCancellableWith DispatchedContinuation.kt:283
    startCoroutineCancellable Cancellable.kt:30
    startCoroutineImpl Builders.common.kt:192
    start_hfyz87_k$ Builders.kt:28
    async Builders.common.kt:91
    promise Promise.kt:30
    promise$default Promise.kt:25
    findById JsTodosApi.kt:19
    loadTodo App.tsx:27
    React 14
    unstable_runWithPriority scheduler.development.js:468
    React 4
HttpStatement.kt:104

```

In the linked repo there is also another branch where I instead added the dependencies `a-client -> common-client` and `b-client -> common-client` and then create two HttpClients by using the factoryMethods of the common-client bundled with a-client (respectively b-client), which works fine, but also doesn’t allow me to share the same HttpClient with a-client and b-client.  
Is this possible at all? If so, can you give me some pointers here?

---

<div class="post-metadata">

**Author:** ![qwert](https://avatars.discourse-cdn.com/v4/letter/q/7bcc69/32.png) [@qwert](https://discuss.kotlinlang.org/u/qwert)\
**Post date:** [January 31, 2022, 6:35pm UTC](https://discuss.kotlinlang.org/t/how-to-share-code-between-multiple-multiplatform-modules-used-in-js/24083/2 "2022-01-31T18:35:07Z")

</div>

Alright, so I came up with this: [GitHub - Fabian-G/shared-mpp at meta\_client](https://github.com/Fabian-G/shared-mpp/tree/meta_client).  
Basically I added yet another module, which just pulls in a-client, b-client and common-client as dependency and therefore exposes them as a single bundle to the frontend. While this enables me to do what I want, it looks rather odd to me.  
Feels like I’m doing something fundamentally wrong here tbh. So any thoughts on this would be very welcome.
