JetBrains / kotlin-wrappers

Kotlin wrappers for popular JavaScript libraries
Apache License 2.0
1.33k stars 165 forks source link

Compilation error when using `node.process.process` #2221

Closed lukellmann closed 4 months ago

lukellmann commented 4 months ago

I get this compilation error when trying to use node.process.process:

e: file:///.../Main.kt:4:13 When accessing module declarations from UMD, they must be marked by both @JsModule and @JsNonModule
fun main() {
    println(process.env)
}

Am I doing something wrong or is this a problem with kotlin-node? The suggested JsNonModule annotation is missing on process: https://github.com/JetBrains/kotlin-wrappers/blob/6444bd21d4146d7d9115c5300d14f573c92e720a/kotlin-node/src/jsMain/kotlin/node/process/process.export.kt

turansky commented 4 months ago

For Node.js applications you need use CommonJS or ES module (both modular).

kotlin {
    js {
        useCommonJs()
        // useEsModules() (fine default for Node and Browser)
    }
}

also you configure module kind like here

lukellmann commented 4 months ago

The usage is in context of a Kotlin/JS library targetting nodejs. Is choosing one of CommonJS or ES modules instead of UMD going to impact Kotlin/JS applications using the library? I.e. will it force the chosen module system on them?

turansky commented 4 months ago

I.e. will it force the chosen module system on them?

Partially. You can force modular module system (CommonJS or ESM) via declarations - like we do. Module kind of library isn't important, because it doesn't affect klib.

In Kotlin Wrappers we support modular declarations only.

@JsNonModule means, that your library is available in global. But how it will be called?

lukellmann commented 4 months ago

I.e. will it force the chosen module system on them?

Partially. You can force modular module system (CommonJS or ESM) via declarations - like we do. Module kind of library isn't important, because it doesn't affect klib.

Alright, this doesn't apply to my case, the Kotlin/JS library only exports Kotlin declarations (which are handled by klib if I understand correctly) and doesn't have any public external declarations. So I'll choose useCommonJs().

Should I close this issue?