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

Kotlin-JS端无法创建HttpClient实例,抛出NoSuchElementException

Fixing NoSuchElementException (List is empty) When Creating HttpClient in Kotlin/JS

That NoSuchElementException: List is empty error you're hitting is almost certainly caused by Dead Code Elimination (DCE) stripping out the Ktor HttpClient's JavaScript engine implementation. When the client tries to initialize, it can't find any available HTTP engines, hence the empty list error. Let's walk through the fixes:

Solution 1: Fix Your DCE Keep Rules

The DCE rule you added doesn't cover all the necessary Ktor client components. Update your build.gradle to include these more comprehensive rules to prevent critical code from being removed:

js {
    browser {
        dceTask {
            // Preserve the JS HTTP engine implementation
            keep("io.ktor.client.engine.js.*")
            // Keep core HttpClient inline dependencies
            keep("ktor-ktor-client-core.$$importsForInline$$.ktor-ktor-client-core.io.ktor.client.*")
            // Retain IO-related inline utilities
            keep("ktor-ktor-io.$$importsForInline$$.ktor-ktor-io.io.ktor.utils.io.*")
        }
    }
}

Solution 2: Upgrade to Newer, Compatible Versions

Your current setup uses Kotlin 1.3.72 and Ktor 1.3.2, which are quite outdated and have known DCE compatibility issues. Upgrading to more recent stable versions will likely resolve the problem entirely, plus give you access to bug fixes and new features.

Here's how to update your version constants and dependencies:

plugins {
    id 'org.jetbrains.kotlin.multiplatform' version '1.6.21'
    id 'org.jetbrains.kotlin.plugin.serialization' version '1.6.21'
    id 'distribution'
}

def ktor_version = '1.6.8'
def logback_version = '1.2.11'
def serialization_version = '1.3.3'
def coroutines_version = '1.6.4'

Then update your jsMain dependencies to match:

jsMain {
    dependencies {
        implementation npm ("text-encoding", "0.7.0")
        implementation npm ("bufferutil", "4.0.7")
        implementation npm ("utf-8-validate", "5.0.10")
        implementation npm ("abort-controller", "3.0.0")
        implementation kotlin('stdlib-js')
        implementation "io.ktor:ktor-client-json-js:$ktor_version"
        implementation "io.ktor:ktor-client-js:$ktor_version"
        implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$coroutines_version"
        implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serialization_version"
    }
}

Quick Additional Checks

  • You don't need to manually specify an engine for JS—Ktor will use JsClient by default, but only if DCE doesn't strip it away.
  • After making changes, run ./gradlew clean to clear any cached build artifacts before rebuilding. This ensures you're working with fresh code.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 18:43:12