kotlin-wrappers icon indicating copy to clipboard operation
kotlin-wrappers copied to clipboard

Compilation error when using `node.process.process`

Open lukellmann opened this issue 1 year ago • 1 comments

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

lukellmann avatar Mar 03 '24 00:03 lukellmann

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

turansky avatar Mar 03 '24 12:03 turansky

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?

lukellmann avatar Mar 03 '24 16:03 lukellmann

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.

  • It's fine for Browser, because modular sources is preferred option for bundlers like Vite or Webpack. ESM - best variant.
  • It's fine for Node, because it supports only modular formats (CommonJS and ESM).

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

  • It's your local bundler configuration in Browser case.
  • It doesn't work for Node.js because you can't access 100% of Node.js API from global
    • Your application is modular in any case - in compiled JS modules will be used

turansky avatar Mar 03 '24 17:03 turansky

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?

lukellmann avatar Mar 03 '24 18:03 lukellmann