You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Kotlin前端Gradle插件的React项目出现依赖解析错误求助

解决Kotlin/JS React项目中kotlinx-html-js找不到kotlin依赖的问题

我之前在维护类似的Kotlin/JS React项目时也踩过这个坑,结合你给出的错误日志和build.gradle配置来看,问题根源是kotlin-frontend插件没有正确将Kotlin标准库暴露给webpack,导致kotlinx-html-js无法解析kotlin模块。下面是具体的修复步骤:

1. 补充kotlin-frontend的Kotlin标准库版本配置

在你的kotlinFrontend块中添加kotlinStdLibVersion配置,让插件自动处理Kotlin标准库的npm映射:

kotlinFrontend {
    kotlinStdLibVersion = kotlin_version // 新增这一行,关联你的Kotlin版本
    npm {
        dependency("webpack-cli", "v2.0.12")
        dependency("react", "15.6.1")
        dependency("react-dom", "15.6.1")
        dependency("react-router-dom", "4.2.2")
        dependency("jquery")
        dependency("react-markdown")
    }
    sourceMaps = true
    webpackBundle {
        bundleName = "main"
        contentPath = file('src/main/web')
        proxyUrl = "http://localhost:8090"
    }
}

这一步的作用是告诉插件,将对应版本的Kotlin标准库JS文件注册到webpack的模块解析路径中,让kotlinx-html-js能找到kotlin模块。

2. 清理构建缓存并重新构建

缓存文件可能残留了错误的依赖映射,执行以下命令清理并重新构建:

./gradlew clean web:webpack-bundle

3. 可选:调整依赖声明方式(如果上述步骤无效)

如果问题仍存在,可以尝试将compile依赖改为implementation(适配Gradle 4+的依赖配置),同时确保Kotlin标准库的依赖正确:

dependencies {
    implementation project(':common-js')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:0.22.5"
    implementation("org.jetbrains.kotlinx:kotlinx-html-js:0.6.10") { force = true }
    implementation 'org.jetbrains:kotlin-react:16.3.1-pre.27-kotlin-1.2.30'
    implementation "org.jetbrains:kotlin-react-dom:16.3.1-pre.27-kotlin-1.2.30"
}

问题原因解释

你遇到的Module not found: Error: Can't resolve 'kotlin'错误,本质是webpack在解析kotlinx-html-js.js时,找不到名为kotlin的模块——这个模块对应Kotlin标准库的JS输出文件。kotlin-frontend插件默认需要明确指定kotlinStdLibVersion,才能自动完成Kotlin标准库到webpack模块的映射,否则会导致依赖路径缺失。

内容的提问来源于stack exchange,提问作者MarcinM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:29:33