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

🐛 BUG: Uncaught ReferenceError: global is not defined #6084

Closed
dangvanthanh opened this issue Jun 19, 2024 · 13 comments
Closed

🐛 BUG: Uncaught ReferenceError: global is not defined #6084

dangvanthanh opened this issue Jun 19, 2024 · 13 comments
Labels
awaiting reporter response Needs clarification or followup from OP bug Something that isn't working

Comments

@dangvanthanh
Copy link

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 dangvanthanh added the bug Something that isn't working label Jun 19, 2024
@github-project-automation github-project-automation bot moved this to Untriaged in workers-sdk Jun 19, 2024
@agotfredsen82
Copy link

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

@Saganic
Copy link

Saganic commented Jul 1, 2024

+1 seeing this upstream in a nuxthub project

@CarmenPopoviciu
Copy link
Contributor

CarmenPopoviciu commented Jul 19, 2024

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 #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 CarmenPopoviciu added the awaiting reporter response Needs clarification or followup from OP label Jul 19, 2024
@dangvanthanh
Copy link
Author

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.

@CarmenPopoviciu
Copy link
Contributor

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

@dangvanthanh
Copy link
Author

@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

@petebacondarwin petebacondarwin added awaiting Cloudflare response Awaiting response from workers-sdk maintainer team and removed awaiting reporter response Needs clarification or followup from OP labels Jul 30, 2024
@huynamboz
Copy link

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

@thijsw
Copy link

thijsw commented Aug 20, 2024

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

@huynamboz
Copy link

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

@dangvanthanh
Copy link
Author

@thijsw

Thanks. But not working for me, too.

@CarmenPopoviciu
Copy link
Contributor

CarmenPopoviciu commented Sep 9, 2024

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 CarmenPopoviciu added awaiting reporter response Needs clarification or followup from OP and removed awaiting Cloudflare response Awaiting response from workers-sdk maintainer team labels Sep 9, 2024
@dangvanthanh
Copy link
Author

@CarmenPopoviciu

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

@CarmenPopoviciu
Copy link
Contributor

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

@github-project-automation github-project-automation bot moved this from Untriaged to Done in workers-sdk Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reporter response Needs clarification or followup from OP bug Something that isn't working
Projects
Archived in project
Development

No branches or pull requests

7 participants