【问题标题】:How to fix "Its dependency 'kotlinx-coroutines-core' was not found."如何修复“未找到其依赖项 'kotlinx-coroutines-core'。”
【发布时间】:2019-10-22 17:03:26
【问题描述】:

我按照本教程创建了一个 kotlin->js 项目:https://kotlinlang.org/docs/tutorials/javascript/getting-started-gradle/getting-started-with-gradle.html

接下来,我按照以下说明在我的代码中使用协程:https://github.com/kotlin/kotlinx.coroutines/blob/master/README.md#using-in-your-projects

到目前为止一切都很好,代码中没有标记错误,我可以构建我的 js 应用程序而没有任何错误消息。但是,我的 js 脚本没有在浏览器中运行,我在浏览器控制台中收到上述错误消息。 有没有人知道我错过了什么或可能配置错误?

这是我的 build.gradle

group 'de.berlin'
version '1.0-SNAPSHOT'

buildscript {
    ext.kotlin_version = '1.3.31'
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'kotlin2js'

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
    testCompile "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version"
    compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0-M1"
    compile("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.0-M1")
    compile "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.3.0-M1"
}

compileKotlin2Js.kotlinOptions.sourceMap = true
compileKotlin2Js.kotlinOptions.outputFile = "${projectDir}/web/js/myApp.js"
compileKotlin2Js.kotlinOptions.suppressWarnings = false
compileKotlin2Js.kotlinOptions.verbose = true

build.doLast {
    configurations.compile.each { File file ->
        copy {
            includeEmptyDirs = false

            from zipTree(file.absolutePath)
            into "${projectDir}/web/js/lib"
            include { fileTreeElement ->
                def path = fileTreeElement.path
                path.endsWith(".js") && (path.startsWith("META-INF/resources/") || !path.startsWith("META-INF/"))
            }
        }
    }

    copy {
        includeEmptyDirs = false
        from "${buildDir}/resources/main"
        into "${projectDir}/web"
    }
}

clean.doFirst {
    delete "${projectDir}/web"

}

一切都编译没有任何错误消息,但我在浏览器控制台中收到以下错误消息: ""未找到它的依赖项 'kotlinx-coroutines-core'。请检查是否在 '(projectname)' 之前加载了 'kotlinx-coroutines-core'。"

更详细的检查表明 /web/js/lib 仅包含 kotlin.js,不应该 kotlinx-coroutines-core 也存在,因为它是依赖块的一部分,应该在 build.doLast-step 中复制? 我还注意到编译后的 js 文件包含以下内容:

if (typeof kotlin === 'undefined') {
  throw new Error("Error loading module 'myApp'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'myApp'.");
}
if (typeof this['kotlinx-coroutines-core'] === 'undefined') {
  throw new Error("Error loading module 'myApp'. Its dependency 'kotlinx-coroutines-core' was not found. Please, check whether 'kotlinx-coroutines-core' is loaded prior to 'myApp'.");
}

为什么是 this['kotlinx-coroutines-core'] 而不是 kotlinx-coroutines-core(如上一行)?

【问题讨论】:

    标签: javascript gradle kotlin kotlin-coroutines kotlin2js


    【解决方案1】:

    为什么是 this['kotlinx-coroutines-core'] 而不是 kotlinx-coroutines-core(如上一行)?

    因为“kotlinx-coroutines-core”不是有效的 JS 标识符。

    "this['kotlinx-coroutines-core']" 正在测试模块 "kotlinx-coroutines-core" 是否已经加载。

    如果未定义,则表示在执行代码之前您尚未“加载”模块。

    根据您加载 JS 模块的方式,您将需要“require(....)”或拥有对“kotlinx-coroutines-core”模块的 html 脚本引用

    【讨论】:

      【解决方案2】:

      开帖已经一年半多了,但我还是回答一下,以防以后有人遇到同样的问题。

      你必须从 npm 下载“kotlinx-coroutines-core”。 Coroutines JS - install

      【讨论】:

        猜你喜欢
        • 2021-11-24
        • 2019-06-10
        • 2021-03-07
        • 1970-01-01
        • 2022-08-03
        • 1970-01-01
        • 1970-01-01
        • 2021-03-17
        • 2019-02-24
        相关资源
        最近更新 更多