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

GraalVM support? ReferenceError: global is not defined #46

Open
helllamer opened this issue Nov 25, 2021 · 6 comments
Open

GraalVM support? ReferenceError: global is not defined #46

helllamer opened this issue Nov 25, 2021 · 6 comments

Comments

@helllamer
Copy link

helllamer commented Nov 25, 2021

Just a small feature request:
Add support for undefined global namespace, if js-runtime environment is not a browser and not a nodejs.

TL;DR

// webpackBootstrap
require("raf");
org.graalvm.polyglot.PolyglotException: ReferenceError: global is not defined
	at <js>.:program(webpack://ScalaJSBundlerLibrary/./node_modules/raf/index.js?:2)

Workaround

var global = {};
// run webpack stuff:
require("raf");
// webpack import ok

Full GraalVM backtrace

Mostly useless.

EXPAND BACKTRACES
Exception in thread "ScalaGraal-pool-1-thread-1" japgolly.scalagraal.ExprError$EvalError: ReferenceError: global is not defined
	at japgolly.scalagraal.ExprError$InEval$.unapply(ExprError.scala:23)
	at japgolly.scalagraal.Expr$.$anonfun$apply$2(Expr.scala:168)
	at japgolly.scalagraal.Expr.$anonfun$$greater$greater$1(Expr.scala:144)
	at japgolly.scalagraal.Expr.evalOrThrow(Expr.scala:25)
	at japgolly.scalagraal.GraalContext$Builder.evalOrThrow$1(GraalContext.scala:131)
	at japgolly.scalagraal.GraalContext$Builder.$anonfun$build$3(GraalContext.scala:134)
	at japgolly.scalagraal.GraalContext$Builder.$anonfun$build$3$adapted(GraalContext.scala:134)
	at scala.Option.foreach(Option.scala:437)
	at japgolly.scalagraal.GraalContext$Builder.build(GraalContext.scala:134)
	at japgolly.scalagraal.GraalContextPool$Builder$Fixed$Step2A.$anonfun$build$1(GraalContextPool.scala:84)
	at scala.Function1.$anonfun$andThen$1(Function1.scala:85)
	at japgolly.scalagraal.GraalContextPool$Builder$Fixed$Step2B.$anonfun$build$2(GraalContextPool.scala:96)
	at japgolly.scalagraal.GraalContextPool$DefaultContextThread$$anon$2.run(GraalContextPool.scala:164)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: org.graalvm.polyglot.PolyglotException: ReferenceError: global is not defined
	at <js>.:program(webpack://ScalaJSBundlerLibrary/./node_modules/raf/index.js?:2)
	at <js>../node_modules/raf/index.js(showcasessr-fastopt-library.js:4941)
	at <js>.__webpack_require__(showcasessr-fastopt-library.js:5808)
	at <js>.:program(webpack://ScalaJSBundlerLibrary/./node_modules/react-motion/lib/Motion.js?:31)
	at <js>../node_modules/react-motion/lib/Motion.js(showcasessr-fastopt-library.js:5050)
	at <js>.__webpack_require__(showcasessr-fastopt-library.js:5808)
	at <js>.:program(webpack://ScalaJSBundlerLibrary/./node_modules/react-motion/lib/react-motion.js?:7)
	at <js>../node_modules/react-motion/lib/react-motion.js(showcasessr-fastopt-library.js:5116)
	at <js>.__webpack_require__(showcasessr-fastopt-library.js:5808)
	at <js>.:anonymous(webpack://ScalaJSBundlerLibrary/./node_modules/react-stonecutter/lib/react-stonecutter.js?:1)
	at org.graalvm.sdk/org.graalvm.polyglot.Context.eval(Context.java:379)
	... 13 more
Exception in thread "ScalaGraal-pool-1-thread-2" japgolly.scalagraal.ExprError$EvalError: ReferenceError: global is not defined
	at japgolly.scalagraal.ExprError$InEval$.unapply(ExprError.scala:23)
	at japgolly.scalagraal.Expr$.$anonfun$apply$2(Expr.scala:168)
	at japgolly.scalagraal.Expr.$anonfun$$greater$greater$1(Expr.scala:144)
	at japgolly.scalagraal.Expr.evalOrThrow(Expr.scala:25)
	at japgolly.scalagraal.GraalContext$Builder.evalOrThrow$1(GraalContext.scala:131)
	at japgolly.scalagraal.GraalContext$Builder.$anonfun$build$3(GraalContext.scala:134)
	at japgolly.scalagraal.GraalContext$Builder.$anonfun$build$3$adapted(GraalContext.scala:134)
	at scala.Option.foreach(Option.scala:437)
	at japgolly.scalagraal.GraalContext$Builder.build(GraalContext.scala:134)
	at japgolly.scalagraal.GraalContextPool$Builder$Fixed$Step2A.$anonfun$build$1(GraalContextPool.scala:84)
	at scala.Function1.$anonfun$andThen$1(Function1.scala:85)
	at japgolly.scalagraal.GraalContextPool$Builder$Fixed$Step2B.$anonfun$build$2(GraalContextPool.scala:96)
	at japgolly.scalagraal.GraalContextPool$DefaultContextThread$$anon$2.run(GraalContextPool.scala:164)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: org.graalvm.polyglot.PolyglotException: ReferenceError: global is not defined
	at <js>.:program(webpack://ScalaJSBundlerLibrary/./node_modules/raf/index.js?:2)
	at <js>../node_modules/raf/index.js(showcasessr-fastopt-library.js:4941)
	at <js>.__webpack_require__(showcasessr-fastopt-library.js:5808)
	at <js>.:program(webpack://ScalaJSBundlerLibrary/./node_modules/react-motion/lib/Motion.js?:31)
	at <js>../node_modules/react-motion/lib/Motion.js(showcasessr-fastopt-library.js:5050)
	at <js>.__webpack_require__(showcasessr-fastopt-library.js:5808)
	at <js>.:program(webpack://ScalaJSBundlerLibrary/./node_modules/react-motion/lib/react-motion.js?:7)
	at <js>../node_modules/react-motion/lib/react-motion.js(showcasessr-fastopt-library.js:5116)
	at <js>.__webpack_require__(showcasessr-fastopt-library.js:5808)
	at <js>.:anonymous(webpack://ScalaJSBundlerLibrary/./node_modules/react-stonecutter/lib/react-stonecutter.js?:1)
	at org.graalvm.sdk/org.graalvm.polyglot.Context.eval(Context.java:379)
	... 13 more
@ljharb
Copy link
Contributor

ljharb commented Nov 25, 2021

How does graal have require but not node’s other globals?

@helllamer
Copy link
Author

I'm not a specialist in js/require/webpack.
Just trying to run webpack-bundled js-file for SSR via GraalVM polyglot
All else dependencies (like react, etc) don't throw such exceptions.

GraalVM officially have several (or more?) options for JS execution:

@ljharb
Copy link
Contributor

ljharb commented Nov 25, 2021

Webpack 5 is broken, and doesn’t polyfill node globals by default. What you’ll want to do, instead of setting global to an empty object, is replace all instances of a global global in the bundle with globalThis (assuming graal supports that).

@helllamer
Copy link
Author

Thanks. I need some time to understand and apply your information to make further steps.
This issue should be closed, by now?

@jbeuckm
Copy link

jbeuckm commented May 29, 2023

We have to hack around this. Any update? Building web workers with Vite and global is undefined.

@ljharb
Copy link
Contributor

ljharb commented May 30, 2023

@jbeuckm if vite won't polyfill it properly by default, then you'd have to either configure it to do so, or switch to a working bundler.

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

3 participants