Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilation error when using node.process.process #2221

Closed
lukellmann opened this issue Mar 3, 2024 · 4 comments
Closed

Compilation error when using node.process.process #2221

lukellmann opened this issue Mar 3, 2024 · 4 comments
Assignees

Comments

@lukellmann
Copy link

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
Copy link
Collaborator

turansky commented Mar 3, 2024

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 turansky self-assigned this Mar 3, 2024
@lukellmann
Copy link
Author

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
Copy link
Collaborator

turansky commented Mar 3, 2024

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

@lukellmann
Copy link
Author

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?

@turansky turansky closed this as completed Mar 3, 2024
lukellmann added a commit to kordlib/kord that referenced this issue Mar 4, 2024
* Gradle 8.5 -> 8.6
* Kotlin 1.9.21 -> 1.9.22
* Ktor 2.3.7 -> 2.3.9
* kotlinx.coroutines 1.7.3 -> 1.8.0
* kotlinx.serialization 1.6.2 -> 1.6.3
* kotlin-logging 6.0.1 -> 6.0.3
* SLF4J 2.0.9 -> 2.0.12
* kotlin-node 18.16.12-pre.619 -> 20.11.5-pre.706
* kotlin-multiplatform-bignum 0.3.8 -> 0.3.9
* KSP 1.9.21-1.0.16 -> 1.9.22-1.0.17
* KotlinPoet 1.15.3 -> 1.16.0
* JUnit 5 5.10.1 -> 5.10.2
* MockK 1.13.8 -> 1.13.10
* Dokka 1.9.10 -> 1.9.20
* AtomicFU 0.23.1 -> 0.23.2
* Binary compatibility validator 0.13.2 -> 0.14.0
* gradle-buildconfig-plugin 5.1.0 -> 5.3.5
* Foojay Toolchains Plugin 0.7.0 -> 0.8.0
* gradle/wrapper-validation-action v1 -> v2
* Replace gradle/gradle-build-action with gradle/actions/setup-gradle
* Apply useCommonJs() to fix issues with kotlin-node, see
  JetBrains/kotlin-wrappers#2221
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants