workers-sdk
workers-sdk copied to clipboard
🐛 BUG: Uncaught ReferenceError: global is not defined
Which Cloudflare product(s) does this pertain to?
Pages
What version(s) of the tool(s) are you using?
Wrangler
What version of Node are you using?
22.3.0
What operating system and version are you using?
Mac Sonoma 14.5
Describe the Bug
Observed behavior
When run npx wrangler pages deploy dist. It's deployment failed.
Expected behavior
Deployment success.
Steps to reproduce
I running build and deploy using Nuxt 3.12.2. It's build sucessfully but can not deployment to cloudflare.
pnpm run build && wrangler pages deploy
Please provide a link to a minimal reproduction
No response
Please provide any relevant error logs
No response
have this aswell . prevents fetch from working any workaround for this ??
+1 seeing this upstream in a nuxthub project
hi @dangvanthanh
The issue at hand here is that global is not exposed in the Workers Runtime by default, only globalThis is. I'm not sure how global is referenced in your case, whether you reference it directly in your Functions code, or if it comes from a third-party dependency. Regardless, there are things that can help out here:
- use
globalThisinstead ofglobalif you are referencingglobalyourself, or useglobalThis.global = globalThisin your Functions code if you don't have direct access to the code that referencesglobal - use the experimental
experimental:nodejs_compat_v2compatibility flag which exposesglobaldirectly
- either in your
wrangler.tomlfile if you use one
# wrangler.toml
compatibility_flags = ["experimental:nodejs_compat_v2"]
- or passed as
wrangler pages deploy [directory] --compatibility-flags="experimental:nodejs_compat_v2"
A big word of warning around using experimental:nodejs_compat_v2 though, since this is very experimental and not recommended for production applications. It also seems to have a bug when used for Pages at the moment (see https://github.com/cloudflare/workers-sdk/issues/6288), which we'll need to look into and fix.
My recommendation is to try and use the first option if possible, and let us know if that helped!
Thanks so much @CarmenPopoviciu
I try 2 solutions but I can't deployment on Cloudflare now.
I see when I use with some package such as argon2...I can't deployment, too.
Now I try check these packages I use, it take a long time. It's maybe error when I use some packages. Because I was deployed successfully before.
@dangvanthanh any chance you have a reproduction at hand we could look at?
@CarmenPopoviciu
I created repository for this issue: https://github.com/dangvanthanh/nuxt-to-pages
$ cd apps/web
$ bun run deploy
The issue when deploy:
same issue with nuxt3, deploy to vercel work but cloudflare not
For me, this issue is resolved when I add vue-router as an explicit dependency, see: https://github.com/nuxt/nuxt/issues/28248#issuecomment-2241749620
For me, this issue is resolved when I add
vue-routeras an explicit dependency, see: nuxt/nuxt#28248 (comment)
Not working for me
@thijsw
Thanks. But not working for me, too.
hi @dangvanthanh
thank you so much for putting that repro repository together 🙏 . I tested it locally by running
cd apps/webnpm run buildnpx wrangler pages dev/npx wrangler pages deploy
and everything worked fine for me 👇
In fact, this works for me with or without adding compatibility_flags = ["nodejs_compat_v2"] in the toml file. I'm wondering if the issue you're experiencing is because you're running deploy via bun?
Can you please confirm whether deploying via wrangler pages deploy works for you 🙏 ?
@CarmenPopoviciu
Now, it's working for me. Even without adding compatibility_flags = ["nodejs_compat_v2"] in wrangler.toml
fantastic!
In case you run into similar Nodejs compat issues, have a look at https://blog.cloudflare.com/more-npm-packages-on-cloudflare-workers-combining-polyfills-and-native-code/ which was just published. There are more details in there about the nodejs_compat_v2 compat flag and our plans regarding Nodejs API support. Hope that helps <3
Closing this issue as it looks like all works as expected now