Kotlin JS error Ktor client resolving 'fs'

382 views Asked by At

I'm trying to build a Kotlin JS application targeting the browser.

I wan't to request a REST API with Ktor Client, Kotlin Serialization and Coroutines. My build.gradle.kts look like this:

dependencies {
    val serializationVersion = "0.20.0"
    val ktorVersion = "1.3.1"
    implementation(kotlin("stdlib-js"))
    implementation("io.ktor:ktor-client-core:$ktorVersion")
    implementation("io.ktor:ktor-client-js:$ktorVersion")
    implementation("io.ktor:ktor-client-serialization-js:$ktorVersion")
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$serializationVersion")
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serializationVersion")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.3.3")
    implementation(npm("abort-controller", "3.0.0"))
    implementation(npm("bufferutil", "4.0.1"))
    implementation(npm("utf-8-validate", "5.0.2"))
    implementation(npm("fs", "0.0.1-security"))
    implementation(npm("text-encoding", "0.7.0"))
}

kotlin.target.browser {
    dceTask {
        dceOptions {
            keep("ktor-ktor-io.\$\$importsForInline\$\$.ktor-ktor-io.io.ktor.utils.io")
        }
    }
}

When I try to run the application by calling ./gradlew run, I'm getting following error on the console:

WARNING in /PATH_TO_PROJECT_DIRECTORY/build/js/packages_imported/ktor-ktor-client-core/1.3.1/ktor-ktor-client-core.js 16700:13-32
Critical dependency: the request of a dependency is an expression
 @ ./kotlin/MyProject.js
 @ multi ./kotlin/MyProject.js

ERROR in /PATH_TO_PROJECT_DIRECTORY/build/js/node_modules/node-gyp-build/index.js
Module not found: Error: Can't resolve 'fs' in '/PATH_TO_PROJECT_DIRECTORY/build/js/node_modules/node-gyp-build'
 @ /PATH_TO_PROJECT_DIRECTORY/build/js/node_modules/node-gyp-build/index.js 1:9-22
 @ /PATH_TO_PROJECT_DIRECTORY/build/js/node_modules/utf-8-validate/index.js
 @ /PATH_TO_PROJECT_DIRECTORY/build/js/node_modules/ws/lib/Validation.js
 @ /PATH_TO_PROJECT_DIRECTORY/build/js/node_modules/ws/lib/Receiver.js
 @ /PATH_TO_PROJECT_DIRECTORY/build/js/node_modules/ws/index.js
 @ /PATH_TO_PROJECT_DIRECTORY/build/js/packages_imported/ktor-ktor-client-core/1.3.1/ktor-ktor-client-core.js
 @ ./kotlin/MyProject.js
 @ multi ./kotlin/MyProject.js

I already tried to remove implementation(npm("fs", "0.0.1-security")) but it don't make any differences.

Does anybody have a clue?

Thanks

1

There are 1 answers

2
elcolto On

Sorry für mit giving sufficient information of gradle File and Project. I was able to solve the problem by downgrading serializationVersion to0.14.0. which ist supported by ktor Client.