oracle / graaljs

GraalJS – A high-performance, ECMAScript compliant, and embeddable JavaScript runtime for Java
https://www.graalvm.org/javascript/
Universal Permissive License v1.0
1.82k stars 191 forks source link

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

Open Strydom opened 3 weeks ago

Strydom commented 3 weeks ago

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/react-dom@18.3.1_react@18.3.1/node_modules/react-dom/cjs/react-dom-server.node.development.js
var require_react_dom_server_node_development = __commonJS({
  "node_modules/.pnpm/react-dom@18.3.1_react@18.3.1/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 commented 2 weeks ago

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