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

Cannot load module: 'util' even though it is polyfilled #866

Open
Strydom opened this issue Oct 30, 2024 · 3 comments
Open

Cannot load module: 'util' even though it is polyfilled #866

Strydom opened this issue Oct 30, 2024 · 3 comments

Comments

@Strydom
Copy link

Strydom commented Oct 30, 2024

When bundling a react component using esbuild and trying to execute it within the JS context, it is unable to require the util package even though it is provided.

private val context: Context = Context
    .newBuilder("js")
    .allowAllAccess(true)
    .allowHostAccess(HostAccess.ALL)
    .allowIO(IOAccess.ALL)
    .option("js.commonjs-require", "true")
    .option("js.commonjs-require-cwd", "path/to/node_modules")
    .option("js.commonjs-core-modules-replacements", "stream:stream-browserify")
    .build()

This throws an error

context.eval(serverScript)

But this does not

context.eval("js", """
    const process = { env: { NODE_DEBUG: false } }
    const util = require('util')
""".trimIndent())

The part of the serverScript throwing the error TypeError: Cannot load module: 'util'

// node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-server.node.development.js
var require_react_dom_server_node_development = __commonJS({
  "node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-server.node.development.js"(exports2) {
    "use strict";
    init_process();
    if (process.env.NODE_ENV !== "production") {
      (function() {
        "use strict";
        var React3 = require_react();
        var util = require("util");

esbuild

await build({
    entryPoints: [
        'components/**/Server*'
    ],
    bundle: true,
    outdir: '../src/main/resources/components/server',
    platform: "node",
    format: "cjs",
    inject: [
        './process.js'
    ]
})
@woess
Copy link
Member

woess commented Nov 7, 2024

I don't see where the util package is provided in your example.

@Strydom
Copy link
Author

Strydom commented Dec 14, 2024

Doesn't js.commonjs-core-modules-replacements provide that?

@woess
Copy link
Member

woess commented Dec 14, 2024

Doesn't js.commonjs-core-modules-replacements provide that?

Only if you provide an explicit mapping, but it might still be found in node_modules anyway which I think is what happens in the working snippet.
Maybe try to see what happens if you set js.commonjs-core-modules-replacements to stream:stream-browserify,util:util.
If that still does not work, I think we need a reproducer to figure out why it can't be loaded.

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