简体   繁体   English

如何修复“未找到它的依赖项 'kotlinx-coroutines-core'。”

[英]How to fix "Its dependency 'kotlinx-coroutines-core' was not found."

I followed this tutorial to create a kotlin->js project: https://kotlinlang.org/docs/tutorials/javascript/getting-started-gradle/getting-started-with-gradle.html我按照本教程创建了一个 kotlin->js 项目: https ://kotlinlang.org/docs/tutorials/javascript/getting-started-gradle/getting-started-with-gradle.html

Next, I followed these instructions to use coroutines in my code: https://github.com/kotlin/kotlinx.coroutines/blob/master/README.md#using-in-your-projects接下来,我按照这些说明在我的代码中使用协程: https : //github.com/kotlin/kotlinx.coroutines/blob/master/README.md#using-in-your-projects

Everything was fine to far, no errors marked in the code and I could build my js application without any error messages.到目前为止一切都很好,代码中没有标记错误,我可以构建我的 js 应用程序而没有任何错误消息。 However, my js scripts are not running in the browser and I get the above mentioned error message in the browser console.但是,我的 js 脚本没有在浏览器中运行,我在浏览器控制台中收到了上述错误消息。 Does any of you here know what I missed or might have configured wrong?你们中有人知道我错过了什么或可能配置错误吗?

Here's my build.gradle这是我的 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"

}

Everything compiles without any error message but I get the following error message in the browser console: ""Its dependency 'kotlinx-coroutines-core' was not found.一切都编译没有任何错误消息,但我在浏览器控制台中收到以下错误消息:“”未找到它的依赖项 'kotlinx-coroutines-core'。 Please, check whether 'kotlinx-coroutines-core' is loaded prior to '(projectname)'."请检查是否在 '(projectname)' 之前加载了 'kotlinx-coroutines-core'。”

A more detailled inspection shows that /web/js/lib only contains kotlin.js, shouldn't kotlinx-coroutines-core be also there because it's part of the depencency block and schould be copied in the build.doLast-step?更详细的检查表明,/web/js/lib 只包含 kotlin.js,kotlinx-coroutines-core 不应该也在那里,因为它是依赖块的一部分,应该在 build.doLast-step 中复制吗? I also noticed that the comiled js file contains the following:我还注意到编译的 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'.");
}

Why is it this['kotlinx-coroutines-core'] and not kotlinx-coroutines-core (like in the line above)?为什么是这个 ['kotlinx-coroutines-core'] 而不是 kotlinx-coroutines-core(如上一行)?

Why is it this['kotlinx-coroutines-core'] and not kotlinx-coroutines-core (like in the line above)?为什么是这个 ['kotlinx-coroutines-core'] 而不是 kotlinx-coroutines-core(如上一行)?

Because "kotlinx-coroutines-core" is not a valid JS identifier.因为“kotlinx-coroutines-core”不是有效的 JS 标识符。

"this['kotlinx-coroutines-core']" is testing whether the module "kotlinx-coroutines-core" has been loaded or not. “this['kotlinx-coroutines-core']”正在测试模块“kotlinx-coroutines-core”是否已加载。

if it is undefined, then it means you have not 'loaded' the module prior to the code being executed.如果未定义,则表示在执行代码之前您尚未“加载”模块。

depending how you load JS modules, you will need to "require(....)" or have an html script reference to the "kotlinx-coroutines-core" module根据您加载 JS 模块的方式,您将需要“require(....)”或具有对“kotlinx-coroutines-core”模块的 html 脚本引用

It has been more than a year and a half since the thread was opened but I answer in case someone has the same problem in the future.自从线程打开以来已经一年半多了,但我会回答以防将来有人遇到同样的问题。

You have to download "kotlinx-coroutines-core" from npm.你必须从 npm 下载“kotlinx-coroutines-core”。 Coroutines JS - install协程 JS - 安装

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM