useCommonJs() is marked as an unresolved reference...
# javascript
g
useCommonJs() is marked as an unresolved reference when I try to use it to compile js in kotlin multiplatform 1.4-M1.
Copy code
js {
    useCommonJs()
    produceExecutable()
    browser {}
}
Is there a way to compile typescript declarations in commonJs format in kotlin multiplatform 1.4-M1?
d
You can still manually set it to output commonjs. Not sure why the helper function doesn't work in 1.4-M1
j
How do you manually set it?
d
Copy code
target {
        browser {
            webpackTask {
                output.libraryTarget = COMMONJS 
                //output.libraryTarget = "commonjs" // alternative
             }
        }
    }
This was from here https://kotlinlang.org/docs/reference/js-modules.html, so if you're in a multiplatform project you'd have
js
instead of
target
j
Thanks! Is it the same if I’m using nodejs()? This is the config I currently have in `build.gradle.kts`:
Copy code
kotlin {
    {...}

    js("js") {
        nodejs()

        compilations["main"].defaultSourceSet.dependencies {
            implementation(kotlin("stdlib-js", kotlin_version))
        }
    }
}
d
It doesn't seem like
nodejs
has a
webpackTask
thing, but this will also work and apply it to all js targets (it's what
useCommonJs
does under the hood).
Copy code
compilations.all {
   it.compileKotlinTask.kotlinOptions {
       moduleKind = "commonjs"
       sourceMap = true
       sourceMapEmbedSources = null
   }
}
j
Thanks Derek. Now I have a new error that suggests maybe I’m on the right track at least?
Copy code
> Task :compileProductionKotlinJs FAILED
e: java.lang.AssertionError: Properties without fields are not supported com.project.models.Flow.Companion_instance
This is `Flow`:
Copy code
@Serializable
@CommonJsExport
data class Flow(
    val id: String, 
    val variables: Variables,           
    val locales: Locales,               
    val data: FlowData
)
Best guess is that the serializable annotation is autogen’ing a companion object that doesn’t play nice with typescript def generation?
d
If you create a property without a backing field in some other class, does it complain about that too? I'm not really sure what the limitations of the IR backend/TypeScript definition generation are
j
It looks like there’s an issue already created about this conflict with @Serialization. I was really hoping to use this for a work project which needs to be delivered relatively soon. Hopefully there’s either a workaround or a fix for this in time.