Open Strydom opened 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.
require
util
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'
serverScript
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' ] })
I don't see where the util package is provided in your example.
When bundling a react component using esbuild and trying to execute it within the JS context, it is unable to
require
theutil
package even though it is provided.This throws an error
But this does not
The part of the
serverScript
throwing the errorTypeError: Cannot load module: 'util'
esbuild