JS lib import problem

Hello everyone.
I’m trying to solve the import problem in Kotlin/JS for browser project.

So, I have previously created. JAR file with only one function (I’ve created jar file, inside this lib are two files, .js and meta.js files )

fun myName():String{
return “myName”
}

I’m trying to import this .jar lib in my project over File->Project Structure->Libraries… and after that, for a specific module I’ve added dependency on that .jar file.

in main.kt file I added myName() function from lib I tried to build project.
An error I’ve got is “unresolved reference: myName”.

I am using the latest version on community IntelliJ IDEA 2020.1.1.

Hi alzomins, Welcome to Kotlin :slight_smile:

I’d like to recommend that we publish and consume your JS Kotlin Library through Gradle.

I’ve attached below a sample library and consumer project. Both projects are basically the kotlin project template for Kotlin JS Browser except for a few differences.

I noticed that in your screenshot you use kotlin script for your gradle script, the projects I attached use that as well.

  1. on the library project we’ll add maven-publish to the plugins.
  2. Run gradle publishToMavenLocal from your library project.
  3. In your app project, add a dependency to your library, for example implementation("org.example:kotlin-js-lib-example:1.0-SNAPSHOT")

KotlinJsLibraryAndAppExample.zip (119.1 KB)

Thanks for the advice nbilyk, I will try.

Also, can You tell me, why I can’t do in the way I’ve mentioned in the initial post? Is there any restriction in importing libs? Or what else can be?

UPDATE:
I am asking this because I am really concerned about data protection in shared/common lib on the repository. It is more, I would like to say, comfortable, to leave data inside the company.

Kind regards

There are a couple reasons to prefer doing it through Gradle.

  1. The publishing plugin ensures that the library is built in the correct way.
  2. If you did get it working by manually adding the jar via IDEA, the dependency would be forgotten next time that the project refreshes.

I am asking this because I am really concerned about data protection in shared/common lib on the repository. It is more, I would like to say, comfortable, to leave data inside the company.

mavenLocal is your local machine, the default location is your user directory at %USER%/.m2/
Publishing your library to somewhere public like sonatype (mavenCentral) is a lot more work :slight_smile:

Hi, I’m afraid IntelliJ IDEA does not update your Gradle dependencies from Project Structure Menu. This approach would work if you were to use built-in IDE build system to build your project.

Although, it is preferred by many to use package repos, and there are ways to setup a company-private repo, you can still reference a .jar file from your build.gradle.kts: implementation(files("path/to/lib.jar"))

1 Like