mordant icon indicating copy to clipboard operation
mordant copied to clipboard

Environment Detection does not work with JS ES Modules

Open robertfmurdock opened this issue 1 year ago • 2 comments

When this module is used as a dependency in a project that uses the setting "compilerOptions { target = "es2015" }", suddenly environment variables aren't available to the application via MultiplatformSystem.readEnvironmentVariable.

It looks like when the JS code tries to determine the environment, it uses a raw 'require'. I believe this is causing an error when inside of an mjs context, causing the system to assume its running in a browser context, even though it is not.

Discovered this after switching a project based on Clikt to use ESM.

robertfmurdock avatar Jan 27 '25 23:01 robertfmurdock

So the import code is actually rather complicated in order to support different environments and avoid compiler warnings: https://github.com/ajalt/mordant/blob/master/mordant/src/jsMain/kotlin/com/github/ajalt/mordant/internal/JsCompat.kt

Do you have any suggestions on how to support that module type?

ajalt avatar Jan 28 '25 18:01 ajalt

I think the async style require might work? though I'm not sure how well that would fit into you existing code. I'll think on it.

robertfmurdock avatar Jan 28 '25 19:01 robertfmurdock