https://kotlinlang.org logo
Join Slack
Powered by
# javascript
  • s

    Stefan Oltmann

    09/01/2025, 8:34 AM
    I'd like people with a good Cloudflare and Kotlin/JS understanding to review (or contribute to) my new project: https://github.com/StefanOltmann/cloudflare-steam-name-update-service I think a lot can be improved here. Especially how I pass in the environment - didn’t find a better way. And I don't like all the
    js()
    interop, too. I like this to be Kotlin/WASM, but the sample (a fork from the original hello world) I found was outdated and didn't work for me. So I appeciate if you drop me hints how to improve or helpful ressources (like sample code) how to properly work with Kotlin/JS (or even better Kotlin/WASM) on Cloudflare Workers. 🙂
    👀 2
    K 1
    t
    • 2
    • 98
  • e

    Edoardo Luppi

    09/01/2025, 3:15 PM
    As far as I recall, there was some talk about un-deprecating the
    @EagerInitialization
    annotation. If that is the case: 1. could it be also applicable to top-level functions? This would be useful for code that has to run when the JS module is initialized. The current workaround is a property that calls a function. 2. could we instead move to an annotation that starts with
    @Js*
    to streamline the naming? For example
    @JsEager
    or
    @JsImmediate
    .
  • g

    grahamborland

    09/02/2025, 11:12 PM
    Trying to upgrade Kotlin from 2.2.0 -> 2.2.10 and started getting this error:
    Copy code
    Execution failed for task ':kotlinNpmInstall'.
    >                 Process 'Resolving NPM dependencies using yarn' returns 1
                      Unknown Syntax Error: Unsupported option name ("--network-concurrency").
    Any workaround?
    v
    e
    a
    • 4
    • 10
  • c

    CLOVIS

    09/03/2025, 7:47 PM
    What would be the correct way to add a dependency if the user has configured a specific feature? For example, I need a specific dependency if the user has defined
    useCommonJs()
    . To detect that, I'm using:
    Copy code
    val moduleKind = project.objects.property<JsModuleKind>()
    	project.tasks.withType<org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrLink> {
    		moduleKind.set(compilerOptions.moduleKind)
    	}
    but by the time
    moduleKind
    is set, it's not allowed to add a
    implementation(npm("foo", "1.0.0"))
    anymore. I've also tried having a
    : RequiresNpmDependencies
    task with a dependency set that changes once
    moduleKind
    is known, and that works without the configuration cache, but with it I get a serialization crash within the configuration cache itself.
    e
    v
    +2
    • 5
    • 30
  • j

    jamshedalamqaderi

    09/04/2025, 9:59 AM
    How can I achieve lazy loading to reduce initialize time of any kotlin js browser app?
    a
    t
    • 3
    • 5
  • t

    Tristan

    09/04/2025, 6:17 PM
    Hello, I have a library that targets both Android and Js. It uses Metro for DI. I compile with
    jsBrowserProductionLibraryDistribution
    Copy code
    js {
            moduleName = project.name
            useEsModules()
            binaries.library()
        }
    I have the following code
    Copy code
    @Inject
    internal class InvocationHandler(
        private val exposedFunctions: Map<String, Provider<ExposedFunction>>,
    ) {
        operator fun invoke(
            onInvocations: SharedFlow<Invocation>,
        ): Flow<Invocation> {
            return onInvocations
                .onEach {
                    val exposedFunction = exposedFunctions[it.location] ?: return@onEach
    
                    it.handle {
                        val fn = exposedFunction()
                        fn(it.parameters) // This throws an error on JS
                    }
                }
        }
    }
    
    // ...
    
    @Inject
    @ContributesIntoMap(AppScope::class)
    @ExposedAtLocation(ExposedFunctionLocation.IS_FILE_CACHED)
    internal class IsFileCached(private val cacheRepository: CacheRepository) : ExposedFunction {
        override suspend fun invoke(args: List<Any>): Any {
            val fileUrl = args[0] as? String ?: return false
            return cacheRepository.isCached(fileUrl)
        }
    }
    For the code above, I will get
    Copy code
    TypeError: fn is not a function
    And if I print with
    fn.toString
    Copy code
    function (_this__u8e3s4, $completion) {
        return i.v8b(_this__u8e3s4, $completion);
      }
    Do you know what could cause this issue?
    a
    e
    z
    • 4
    • 36
  • c

    CLOVIS

    09/05/2025, 6:32 PM
    I'm getting this weird import in the generated
    kotlin-kotlin-stdlib.js
    Copy code
    const DYNAMIC_MODULES_ID = '\0commonjs-dynamic-modules';
    const HELPERS_ID = '\0commonjsHelpers.js';
    Note the
    \0
    . Has anyone seen this before?
    e
    • 2
    • 10
  • a

    Alex Styl

    09/09/2025, 4:13 AM
    Im getting an error
    Could not locate the bindings file
    when I am installing
    sqlite3
    as a npm() dependnecy in build.gradle.kts:
    Copy code
    implementation(npm("sqlite3",  "5.1.7"))
    I think it has something to do with native bindings (C ?) . The library is this one Any pointers on how to use it in kotlin /js ?
    a
    i
    • 3
    • 10
  • c

    Cas Van Luijtelaar

    09/09/2025, 7:22 AM
    running into
    Copy code
    e: org.jetbrains.kotlin.util.FileAnalysisException: Somewhere in file /Users/casvanluijtelaar/Projects/***/deals/DealsOfferKey.kt: java.lang.NullPointerException: null cannot be cast to non-null type org.jetbrains.kotlin.fir.symbols.impl.FirRegularClassSymbol
    	at org.jetbrains.kotlin.util.AnalysisExceptionsKt.wrapIntoFileAnalysisExceptionIfNeeded(AnalysisExceptions.kt:62)
    	at org.jetbrains.kotlin.fir.FirCliExceptionHandler.handleExceptionOnFileAnalysis(Utils.kt:251)
    	at org.jetbrains.kotlin.fir.resolve.transformers.FirSupertypeResolverTransformer.transformFile(FirSupertypesResolution.kt:892)
    	at org.jetbrains.kotlin.fir.declarations.FirFile.transform(FirFile.kt:46)
    	at org.jetbrains.kotlin.fir.resolve.transformers.FirTransformerBasedResolveProcessor.processFile(FirResolveProcessor.kt:48)
    	at org.jetbrains.kotlin.fir.resolve.transformers.FirTotalResolveProcessor.process(FirTotalResolveProcessor.kt:36)
    	at org.jetbrains.kotlin.fir.pipeline.AnalyseKt.runResolution(analyse.kt:24)
    	at org.jetbrains.kotlin.fir.pipeline.FirUtilsKt.resolveAndCheckFir(firUtils.kt:76)
    when I try to build
    jsBrowserDevelopmentLibraryDistribution
    seems to throw on any file with kotlinx-serializable imported. e.g. this file only contains:
    Copy code
    import kotlinx.serialization.Serializable
    
    @Serializable
    data class DealsOfferKey(
        val offerId: Int,
        val offerInstanceUniqueId: String?,
    )
    removing the annotation and the import fixes the issue. but then errors on one of a million other files with serializable included. Anyone seen this before?
    e
    a
    • 3
    • 7
  • e

    Edoardo Luppi

    09/10/2025, 8:55 AM
    2.2.20 is out, and there are a lot of improvements for JS. https://github.com/JetBrains/kotlin/releases/tag/v2.2.20
  • e

    Edoardo Luppi

    09/10/2025, 9:51 AM
    Just an observation now that I've compiled with 2.2.20. The new
    String.substring
    approach is better because it directly calls JS'
    substring
    , however it now produces:
    Copy code
    substring(response, startIndex, endIndex);
    Where
    substring
    is:
    Copy code
    function substring(_this__u8e3s4, startIndex, endIndex) {
      _init_properties_stringJs_kt__bg7zye();
      // Inline function 'kotlin.js.asDynamic' call
      return _this__u8e3s4.substring(startIndex, endIndex);
    }
    If instead you bring back
    inline
    for those two functions https://github.com/JetBrains/kotlin/blob/400e1ecadbef70744b6ee7eb6234a0a6ea8bbda8/libraries/stdlib/js/src/kotlin/text/stringJs.kt#L274-L276 you won't have to pass by the lazy initialization of the file-level properties. A similar situation is valid for
    Long
    too as far as I can see. Alternatively properties could be marked with
    EagerInitialization
    .
    a
    • 2
    • 5
  • m

    MrPowerGamerBR

    09/11/2025, 7:23 PM
    Is it possible to use
    @JsName
    on properties declared on a
    @JsPlainObject
    ? I wanted to change some
    snake_case
    variables into
    camelCase
    , to make it look "better" in Kotlin I tried searching for it, but the only result of someone trying to do this was this conversation: https://kotlinlang.slack.com/archives/C0B8L3U69/p1728428182216839 However after testing it, it doesn't seem to work on Kotlin 2.2.0 😞
    t
    • 2
    • 9
  • m

    Marc

    09/12/2025, 11:41 AM
    Are unsigned types fully supported on JS? I was trying to use them but I ran into a strange error:
    Copy code
    fun main() {
        ubyteArrayOf(
            *listOf(ubyteArrayOf())[0]
        )
    }
    results in:
    Copy code
    Unhandled JavaScript exception: listOf(...).get_c1px32_k$(...).slice is not a function
    https://pl.kotl.in/nEyBqjkBZ The same code works just fine when replacing
    ubyteArrayOf
    with
    byteArrayOf
    or running it on JVM instead.
    👀 2
    e
    • 2
    • 11
  • c

    CLOVIS

    09/13/2025, 4:01 PM
    Worried about bundle size? Here are two options that can help decrease it: • Using ESM modules • Using ES2015 language features Both of these articles give examples of how the options affect the generated code.
    ❤️ 11
    kodee loving 4
    🆒 1
  • e

    edenman

    09/16/2025, 8:43 PM
    Anybody else ran into this issue before? IDE isn’t able to resolve KotlinJsTargetDsl and it makes dev very annoying bc all the JsExport stuff shows red in the editor https://youtrack.jetbrains.com/issue/KT-81089/
    a
    • 2
    • 1
  • a

    Andreas Martin Mørch

    09/19/2025, 12:22 PM
    Anyone else run into an issue where changing the version of an npm dependency (via the Gradle DSL, i.e.
    implementation(npm(package, version))
    does not invalidate build cache? Kotlin 2.2.0, Gradle 8.12 Running build before and after changing the NPM dependency shows the same build cache key for
    compileKotlinJs
    . I would expect this to trigger a failed build and prompt the user to run
    kotlinUpgradeYarnLock
    like normal.
    a
    e
    a
    • 4
    • 4
  • s

    Stefan Oltmann

    09/21/2025, 3:03 AM
    While waiting for AWS SDK Multiplatform I wonder why there a no Kotlin-wrappers available for the official AWS S3 JS lib… isn’t that thing popular?
    a
    t
    • 3
    • 2
  • b

    Bernhard

    09/25/2025, 7:59 AM
    I'm writing type defs and came across
    Copy code
    {GridCoordinates2D|HexagonalGridCube2D|Point} HexagonalGridCoordinates2D
    and wondered, if there was a way to model that in Kotlin. Couldn't you use a marker interface for that? As in: an empty interface that all of these types extend?
    ✅ 1
    e
    t
    • 3
    • 9
  • j

    Jan Biedermann

    09/26/2025, 6:14 AM
    Hello everybody, how can i instruct gradle to get a npm dependency directly from a github repo instead of npmjs.com?
    e
    • 2
    • 9
  • o

    Oliver.O

    09/26/2025, 12:33 PM
    How can I pass a parameter from the Gradle command line to my JS tests (Node, browser, Wasm)? Example: Indicate if tests run on CI.
    v
    e
    +2
    • 5
    • 17
  • l

    Luv Kumar

    09/26/2025, 4:56 PM
    Hi all, today I noticed that there are no null checks for non nullable fields for generated JS code in KMP. I was assuming that the behaviour might have been similar to JVM where generated bytecode has null checks and code fails fast but it is not. How do people overcome this usually ? I can manually add null checks in
    init {}
    block but not only kotlin gives warning for that, it's cumbersome process to add to all exposed classes and would pollute the code as well.
    e
    s
    c
    • 4
    • 7
  • h

    Horatio Thomas

    09/30/2025, 10:31 AM
    Just decided to open source this investment tracker I've been working on as a personal passion project: https://github.com/TENET-THEORY/knance. It is probably the only sample out there that shares code between ktor and next js. Inspired by https://github.com/Kotlin/kmp-fullstack-conference-cms. If you like it, give it a star!
    👀 1
    K 1
    a
    • 2
    • 3
  • n

    Nickolay

    10/03/2025, 10:21 PM
    [kotlin - Why can't I use @JsExport in commonMain? Workaround with expect/actual seems to work fine - Stack Overflow](https://stackoverflow.com/questions/79782181/why-cant-i-use-jsexport-in-commonmain-workaround-with-expect-actual-seems-to)
    t
    e
    • 3
    • 17
  • a

    Artem Kobzar

    10/07/2025, 7:00 AM
    Good morning everyone ☀️ I need your input so much in the following question. In the thread, please share how you publish Kotlin/JS artifacts (not KLIB, but the executable) to a registry (e.g., NPM, JSR, Maven) and any pain points you've encountered with the approach. Thank you for the help 🙏 Artem, Kotlin/JS Lead
    👀 2
    a
    e
    +4
    • 7
    • 17
  • e

    Edoardo Luppi

    10/07/2025, 12:25 PM
    Kotlin 2.2.21-RC is out with a couple of fixes for K/JS. https://github.com/JetBrains/kotlin/releases/tag/v2.2.21-RC
    🎉 7
  • n

    Nickolay

    10/09/2025, 4:19 PM
    [kotlin - Can we expose interfaces from
    commonMain
    to JS that can be implemented in Typescript? - Stack Overflow](https://stackoverflow.com/questions/79786639/can-we-expose-interfaces-from-commonmain-to-js-that-can-be-implemented-in-type)
    t
    • 2
    • 3
  • m

    MrPowerGamerBR

    10/10/2025, 2:12 AM
    How can you create Web Components using Kotlin/JS? I did try to follow this guide and, while it did work in the past, it seems that the
    kotlin-browser
    bindings for Web Components have changed quite a bit, and now I'm not able to get a custom element to work
    a
    t
    a
    • 4
    • 24
  • a

    Alex Styl

    10/13/2025, 9:26 AM
    Major bottleneck for me right now with kotlin/js is the compilation speed. It slows down the dev loop dramatically vs just using javascript. This is while building backends on nodejs Everything else is fantastic
    e
    g
    a
    • 4
    • 19
  • e

    Edoardo Luppi

    10/13/2025, 9:41 AM
    Edit: https://kotlinlang.slack.com/archives/CDFP59223/p1759383119112699 What's up with this warning in 2.2.20? I suppose it's an IDE bug, and I don't have to worry about anything.
    a
    t
    • 3
    • 5
  • n

    Nickolay

    10/13/2025, 9:25 PM
    Is there a way for generated typescript files
    d.ts
    to have KDoc comments transferred over to
    d.ts
    from the Kotlin files?
    b
    a
    • 3
    • 2