NPM dependencies with Kotlin 1.3.40

Hi,

based on the code snippet in the “Experimental support for NPM and Webpack” section at https://blog.jetbrains.com/kotlin/2019/06/kotlin-1-3-40-released/ I’m trying to declare NPM dependencies like

plugins {
    kotlin("js") version "1.3.40"
}

repositories {
    jcenter()
}

dependencies {
    implementation(npm("react", "^16.8.1"))
}

But the npm symbol cannot be found, and I also didn’t find a matching import. How can I make this work?

Thanks!

You need to declare dependencies at kotlin source set, not global dependencies container:

plugins {
    kotlin("js") version "1.3.40"
}

repositories {
    jcenter()
}

kotlin {
    target {
        // You can drop browser or node if you targeting only one
        nodejs()
        browser()
    }

    sourceSets["main"].dependencies {
        implementation(npm("react", "^16.8.1"))
    }
}

If you plan to use multiplatform plugin, you need to use slightly different things:

plugins {
    kotlin("multiplatform") version "1.3.40"
}

repositories {
    jcenter()
}

kotlin {
    js {
        nodejs()
        browser()
    }
    
    jvm {
        // options for jvm
    }

    sourceSets["jsMain"].dependencies {
        implementation(npm("react", "^16.8.1"))
    }
}
3 Likes

Thanks!

Does anyone have module dependencies working for the multiplatform jsTest tasks? My project has multiple modules/projects (they are multiplatform modules). I’m trying to test the topmost module (the one that depends on the others), but the javascript is not even built.

Never mind. It seems that marking them browser and/or nodejs will do the deal.

Alternative way:

nodeJs.packageJson {
    dependencies["react"] = "^16.8.1"
}

For browser deployment, how does one collect these dependencies?

For non-npm dependencies I can get them via the compilation’s runtimeDependencyFiles property, but I can’t resolve that property once I’ve added an npm dependency

(Could not resolve all files for configuration ‘:new-project:jsRuntimeClasspath’.)