workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

🐛 BUG: Uncaught ReferenceError: global is not defined

Open dangvanthanh opened this issue 1 year ago • 10 comments

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

Screenshot 2024-06-19 at 3 06 36 PM

Screenshot 2024-06-19 at 2 57 52 PM

Please provide a link to a minimal reproduction

No response

Please provide any relevant error logs

No response

dangvanthanh avatar Jun 19 '24 08:06 dangvanthanh

have this aswell . prevents fetch from working any workaround for this ??

agotfredsen82 avatar Jun 26 '24 10:06 agotfredsen82

+1 seeing this upstream in a nuxthub project

Saganic avatar Jul 01 '24 14:07 Saganic

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:

  1. use globalThis instead of global if you are referencing global yourself, or use globalThis.global = globalThis in your Functions code if you don't have direct access to the code that references global
  2. use the experimental experimental:nodejs_compat_v2 compatibility flag which exposes global directly
  • either in your wrangler.toml file 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!

CarmenPopoviciu avatar Jul 19 '24 11:07 CarmenPopoviciu

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 avatar Jul 19 '24 16:07 dangvanthanh

@dangvanthanh any chance you have a reproduction at hand we could look at?

CarmenPopoviciu avatar Jul 22 '24 17:07 CarmenPopoviciu

@CarmenPopoviciu

I created repository for this issue: https://github.com/dangvanthanh/nuxt-to-pages

$ cd apps/web
$ bun run deploy

The issue when deploy:

Screenshot 2024-07-23 at 9 36 45 AM

dangvanthanh avatar Jul 23 '24 02:07 dangvanthanh

same issue with nuxt3, deploy to vercel work but cloudflare not image

huynamboz avatar Aug 12 '24 17:08 huynamboz

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

thijsw avatar Aug 20 '24 18:08 thijsw

For me, this issue is resolved when I add vue-router as an explicit dependency, see: nuxt/nuxt#28248 (comment)

Not working for me image image

huynamboz avatar Aug 21 '24 04:08 huynamboz

@thijsw

Thanks. But not working for me, too.

dangvanthanh avatar Aug 21 '24 05:08 dangvanthanh

hi @dangvanthanh

thank you so much for putting that repro repository together 🙏 . I tested it locally by running

  1. cd apps/web
  2. npm run build
  3. npx wrangler pages dev / npx wrangler pages deploy

and everything worked fine for me 👇

Screenshot 2024-09-09 at 15 45 39

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 avatar Sep 09 '24 13:09 CarmenPopoviciu

@CarmenPopoviciu

Now, it's working for me. Even without adding compatibility_flags = ["nodejs_compat_v2"] in wrangler.toml

dangvanthanh avatar Sep 10 '24 02:09 dangvanthanh

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

CarmenPopoviciu avatar Sep 10 '24 07:09 CarmenPopoviciu