Adam S
06/22/2023, 4:14 PMsuspend fun main() {
val libpcre2x = require("jsRequire") as () -> Promise<PcreModule>
val module = libpcre2x().await()
module.loaded.await()
println("pcreModule.version: ${pcreModule.version()}")
// prints: pcreModule.version: 10.42 2022-12-11
}
@JsName("require")
private external fun jsRequire(value: String): dynamic
Why does require()
work on NodeJS, but on browsers it fails with ‘Cannot find module’? Is it possible to fix it?
Error: Cannot find module 'libpcre2x'
at <global>.webpackEmptyContext(/projects/rexkt/build/js/packages/rexkt-rexkt-lib-js-browser-test/kotlin|sync:2)
at protoOf.require_z6w6n0(/projects/rexkt/rexkt-lib/src/jsMain/kotlin/externals/js.kt:8)
at protoOf.doResume_5yljmg(/projects/rexkt/rexkt-lib/src/jsMain/kotlin/externals/pcre2-wasm.extensions.kt:11)
...
Adam S
06/22/2023, 4:28 PM@JsModule
and @JsNonModule
, but this always returns ‘undefined’
@JsModule("libpcre2x")
@JsNonModule
@JsName("default")
//@JsName("libpcre2x")
//@JsName("Module")
external val libpcre2x: dynamic
Adam S
06/22/2023, 4:31 PMlibpcre2x.js
file (generated by emscripten looks like this, so I would expect that I’m able to access the var libpcre2x
Artem Kobzar
06/22/2023, 10:03 PMAdam S
06/22/2023, 10:06 PMkotlin {
sourceSets {
jsMain {
dependencies {
implementation(npm("libpcre2x", file("local_node_modules/libpcre2x")))
}
}
}
}
Artem Kobzar
06/22/2023, 10:06 PMArtem Kobzar
06/22/2023, 10:07 PMAdam S
06/22/2023, 10:10 PM> Task :rexkt-lib:jsBrowserBrowserTest
Module not found: Error: Can't resolve 'fs' in '/projects/rexkt/build/js/node_modules/libpcre2x/dist'
Module not found: Error: Can't resolve 'path' in '/projects/rexkt/build/js/node_modules/libpcre2x/dist'
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: { "path": require.resolve("path-browserify") }'
- install 'path-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "path": false }
Module not found: Error: Can't resolve 'perf_hooks' in '/projects/rexkt/build/js/node_modules/libpcre2x/dist'
404: /libpcre2x.wasm
404: /libpcre2x.wasm
dev.adamko.rexkt.PcreModuleTest.pcreVersion[jsBrowser, browser, ChromeHeadless114.0.5735.133, MacOS10.15.7] STARTED
dev.adamko.rexkt.PcreModuleTest.pcreVersion[jsBrowser, browser, ChromeHeadless114.0.5735.133, MacOS10.15.7] FAILED
RuntimeError at /projects/rexkt/build/js/node_modules/libpcre2x/dist/libpcre2x.js:530
Artem Kobzar
06/22/2023, 10:14 PMAdam S
06/22/2023, 10:15 PMArtem Kobzar
06/22/2023, 10:19 PMArtem Kobzar
06/22/2023, 10:29 PM