Kotlin/JS: browser vs. nodejs

I don’t understand how browser and nodejs are supposed to be used in the build.gradle.kts of a multiplatform project. Sure, if you want to build for only one of these platforms, then you would use only one of them. But what if you want to build for both platforms? Would you use both, like this?

js(IR) {
        browser {
             testTask {
                useKarma {
                    useChromeHeadless()
                }
            }
        }
        nodejs {
            testTask {
                useKarma {
                    useChromeHeadless()
                }
            }
        }
        binaries.library()
    }

Using both has not produced an error or warning in my case, and cleanly separated test results have been generated (with all runs in the same report though).
However, they seem to override each other’s artifacts, with minor differences, none of which seem to indicate the platform though.

It is the current limitation of the build process. The problem is that there is not enough manpower to fix that and not a lot of people use node target.

1 Like

So if I understand you correctly, one has to choose either one. But what is the actual difference between a library for a browser and Node.js? Obviously, it you use APIs that are available only on one platform, it will not work on the other, but apart from that? Why even have slightly different artifacts for browser and nodejs? I think each should still just work on the other platform.

1 Like

You can always make multiple compilations, even if they’re the same platform target. Here’s an example with two browser compilations: avwie's programming blog

1 Like

Thanks. But I guess then I would also have to distribute two separate NPM packages for browser and Node.js? I don’t think I’ve ever seen this done in the NPM repository. The question still remains why to have two different compilations at all.

Also one thing I had been blind to: How does the following actually make sense, when a Node.js compilation should actually be tested (and possibly only be testable) in Node.js?

nodejs {
    testTask {
        useKarma {
            useChromeHeadless() // Node.js in a browser?
        }
    }
}