kotlinx-io: Kotlin/JS-modules no longer work in browser after #256

In #256 I refactored the interop with nodeJs modules providing filesystem support. If previously, only functionality related to files and filesystem didn’t work in a browser (with UnsupportedOperationException being thrown), now an attempt to use anything declared in kotlinx-io-core leads to errors like this:

> Task :gradle-core-multiplatform:jsBrowserTest
Module not found: Error: Can't resolve 'buffer' in '/Users/Filipp.Zhinkin/Development/kotlinx-io-release-tests/build/js/packages/kotlinx-io-release-tests-gradle-core-multiplatform-test/kotlin'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
        - add a fallback 'resolve.fallback: { "buffer": require.resolve("buffer/") }'
        - install 'buffer'
If you don't want to include a polyfill, you can use an empty module like this:
        resolve.fallback: { "buffer": false }
Module not found: Error: Can't resolve 'fs' in '/Users/Filipp.Zhinkin/Development/kotlinx-io-release-tests/build/js/packages/kotlinx-io-release-tests-gradle-core-multiplatform-test/kotlin'
Module not found: Error: Can't resolve 'os' in '/Users/Filipp.Zhinkin/Development/kotlinx-io-release-tests/build/js/packages/kotlinx-io-release-tests-gradle-core-multiplatform-test/kotlin'

It’s a blocker as it makes kotlinx-io-core useless in nodeJs-less environments.

About this issue

  • Original URL
  • State: closed
  • Created 4 months ago
  • Reactions: 4
  • Comments: 15 (2 by maintainers)

Commits related to this issue

Most upvoted comments

This really shows how much we need https://youtrack.jetbrains.com/issue/KT-47038.

This type of issue would be entirely avoidable.

@whyoleg it works, thanks! I’ll incorporate the fix.

@joffrey-bion, right. The plan is to phase it out from core module in subsequent releases.

I’ll check if it could be fixed on the library side

I might have a solution here: instead of js("require(\"path\")") use js("eval('require')('path')"). I’m sure that it works (at least with latest Kotlin versions) for both js and wasm, browser and node without any warnings/errors at my side in cryptography-kotlin (js and wasm), but I’m not sure, how it affects some optimisations, but at least it doesn’t spam users with unrelated to them warnings

By the way, why isn’t the filesystem-related functionality part of another module (not kotlinx-io-core) that doesn’t have the browser among its targets? This would avoid “lying” on the supported platforms, and solve some of those issues, right?

@StefanOltmann v0.3.2 should be deployed to the central repository within an hour.

However, error messages will remain. #283 only solved an issue (introduced in #256) with an actual error being raised a prevented execution within a browser when kotlinx-io was imported.

I’ll investigate it further, but for now, it seems like warnings don’t cause actual problems in runtime.