nuxt-hub / core

Build full-stack applications with Nuxt on CloudFlare, with zero configuration.
https://hub.nuxt.com
Apache License 2.0
993 stars 56 forks source link

[wrangler dev] Can't resolve `node:async_hooks` #273

Closed remihuigen closed 2 months ago

remihuigen commented 2 months ago

Describe the bug Running nuxt build with cloudflare-pages preset, and npx wrangler pages dev dist/ results in

✘ [ERROR] service core:user:worker: Uncaught Error: No such module "node:async_hooks".

    imported from "r4j10pyoufr.js"

✘ [ERROR] MiniflareCoreError [ERR_RUNTIME_FAILURE]: The Workers runtime failed to start. There is likely additional logging output above.

Introduced somewhere after 0.7.9

adamkasper commented 2 months ago

I’m having the same problem when running npx wrangler pages dev dist/. In the NuxtHub admin, I get this error:

Error: Failed to publish your Function. Got error: Uncaught ReferenceError: global is not defined
  at chunks/runtime.mjs:1:30924

In my project, I am only using Supabase, NuxtUI, NuxtHub and Pinia. And I have enabled compatibilityVersion: 4.

{
  "name": "new-app",
  "type": "module",
  "private": true,
  "packageManager": "pnpm@9.10.0",
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev",
    "generate": "nuxt generate",
    "postinstall": "nuxt prepare",
    "preview": "nuxt preview"
  },
  "dependencies": {
    "@antfu/eslint-config": "^3.5.1",
    "@nuxt/eslint": "^0.5.7",
    "@nuxt/ui": "^2.18.4",
    "@nuxthub/core": "^0.7.12",
    "@nuxtjs/supabase": "^1.4.0",
    "@pinia/nuxt": "^0.5.4",
    "@vueuse/router": "^11.0.3",
    "camelcase-keys": "^9.1.3",
    "dayjs": "^1.11.13",
    "eslint-plugin-tailwindcss": "^3.17.4",
    "nuxt": "^3.13.1",
    "pagedjs": "^0.4.3",
    "snakecase-keys": "^8.0.1",
    "vue": "^3.5.4",
    "vue-router": "^4.4.4",
    "zod": "^3.23.8"
  },
  "devDependencies": {
    "sass": "^1.78.0",
    "wrangler": "^3.76.0"
  }
}
export default defineNuxtConfig({
  future: {
    compatibilityVersion: 4,
  },

  modules: [
    '@nuxt/eslint',
    '@nuxthub/core',
    '@nuxt/ui',
    '@nuxtjs/supabase',
    '@pinia/nuxt',
  ],

  eslint: {
    config: {
      standalone: false,
      stylistic: true,
      typescript: true,
    },
  },

  colorMode: {
    preference: 'light',
  },

  supabase: {
    types: '~/types/database.types.ts',
  },

  compatibilityDate: '2023-09-10',

  devtools: { enabled: true },
})
atinux commented 2 months ago

@adamkasper this is not related to the main issue, see https://github.com/nuxt-hub/core/issues/261#issuecomment-2343229143 (should be fixed in upcoming Nuxt release)

atinux commented 2 months ago

Let me have a look @remihuigen

atinux commented 2 months ago

Why do you need to run the npx wrangler pages dev dist/ command @remihuigen ?

remihuigen commented 2 months ago

Sorry, I wasn't clear. Got this error in local dev, not in production

Was debugging as described on https://hub.nuxt.com/docs/recipes/debug

remihuigen commented 2 months ago

To confirm: I'm not getting the error in production

atinux commented 2 months ago

I see, well the debugging is not perfect yes as I don't provide the wrangler.toml, let me track this on https://github.com/nuxt-hub/cli/issues/22

remihuigen commented 2 months ago

Sure, no problem.

Didn't get these errors though when following https://hub.nuxt.com/docs/recipes/debug with v0.7.9, so i'd thought a new issue was in order :)

ra-jeev commented 2 months ago

The issue here is that the preview should be running in nodejs_compat mode which is enabled by default when you deploy it in production.

For local workers you get this warning first while compiling, and then on runtime you get the above error:

 [WARNING] The package "node:async_hooks" wasn't found on the file system but is built into node.

  Your Worker may throw errors at runtime unless you enable the "nodejs_compat" compatibility flag.
  Refer to https://developers.cloudflare.com/workers/runtime-apis/nodejs/ for more details. Imported
  from:

Faced the same issue yesterday while trying to preview the build locally. Not sure how to enable the compat mode locally.

Note: This issue started with v0.7.12 (Async Local Storage release)

atinux commented 2 months ago

As a workaround you can create a wrangler.toml at the root of your project (will be used only for the preview)

 compatibility_flags = ["nodejs_compat_v2"]

Will work on https://github.com/nuxt-hub/cli/issues/22 to fix this

atinux commented 2 months ago

Alright it's live! https://hub.nuxt.com/changelog/nuxthub-preview

It should work now with:

npx nuxthub preview
ra-jeev commented 2 months ago

@atinux This works great. But in the build output I see that the wrangler command is being shown for preview.

But the codebase shows npx nuxthub preview, do I need to update something (I updated NuxtHub CLI and @nuxthub/core)?

Screen Shot 2024-09-17 at 11 07 25 AM
atinux commented 2 months ago

Where do you see the wrangler command @ra-jeev ?

ra-jeev commented 2 months ago

@atinux In the build output (pnpm build). It should mention npx nuxthub preview now, and I presume the code screenshot I pasted above should be doing that?

Screen Shot 2024-09-17 at 4 54 50 PM
atinux commented 2 months ago

My bad, should be good with https://github.com/nuxt-hub/core/releases/tag/v0.7.19