How to transfer KotlinJs dependencies?

I am creating a multiplatform library and have included a dependency in my jsMain and jvmMain sourcesets:

implementation("net.onedaybeard.bitvector:bitvector-js:0.1.4")
implementation("net.onedaybeard.bitvector:bitvector-jvm:0.1.4")

When building the JVM everything works, but for the JS it expects the module bitvector:

if (typeof bitvector === 'undefined') {
  throw new Error("Error loading module 'common'. Its dependency 'bitvector' was not found. Please, check whether 'bitvector' is loaded prior to 'common'.");
}

I know it is part of the JAR file of net.onedaybeard.bitvector:bitvector-js:0.1.4 but what do I need to do to transfer the dependencies to the node_modules of my own library?

I think this should be obtained by doing a Gradle task, but unsure of how to do this.

EDIT
Just when I posted this I saw in build/js/packages_imported/bitvector/0.1.4 the JS files. However, I donā€™t know if it is the correct way to get it from there?

Hi there

The dependency management on K/JS multiplatform is a bit messy, you have two possible ways, either use the nodejs plugin to create a task that will install the dependencies or use a plain old gradle task that will take your dependencies from maven and copy them in the folder manually.

I usually do the first one but you might end up doing the second if your dep is not on the NPM registry.

Hello, thanks for your answer.

Good to know that there isnā€™t a standard approach to this. I guess it might improve in the future.

At the moment I solved it with a custom task that creates my main and test NPM packages and includes all the dependencies there. Seems to work.

Is there an example of ā€˜option 2ā€™?

Option 2 looks a bit like this, assuming your JS module name is ā€œjsDafaultā€:

task copyDepIntoModules(type: Copy, dependsOn: [configurations.jsDefault]){
    outputs.upToDateWhen {false}
    from {
        configurations.jsDefault.filter{ it.path.contains("theDependencyToCopy") }.collect { zipTree(it) }
    }
    into "theDirectoryOfYourNodeProject/node_modules"
}

This task will extract the given dependency from the maven dependencies and copy it to the directory of your choosing. Itā€™s not the most elegant way of doing it but I struggled a lot with it and didnā€™t find the motivation to enhance it.

2 Likes