nuxt-hub / core

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

Cannot access 'tc' before initialization #363

Closed aligzl closed 2 weeks ago

aligzl commented 2 weeks ago

Describe the bug Hi, i cant deploy my app as getting error about hubDatabase during the deployment

17:48:42.892 Error: Failed to publish your Function. Got error: Uncaught ReferenceError: Cannot access 'tc' before initialization at chunks/nitro/nitro.mjs:1:588600 in useRuntimeConfig at chunks/nitro/nitro.mjs:1:225944 in requireNuxtHubFeature at chunks/nitro/nitro.mjs:1:444655 in hubDatabase at chunks/nitro/nitro.mjs:1:458639

Steps to reproduce Steps to reproduce the behavior: Deplyment Logs

atinux commented 2 weeks ago

What is displayed when your run npx nuxi info?

aligzl commented 2 weeks ago

What is displayed when your run npx nuxi info?

aligzl commented 2 weeks ago

i am also getting this error even i didnt update any deps during dev

[worker reload] [worker init] .nuxt/dev/index.mjs failed

i assume that this error comes from nuxt related to #6712 and #2832

aligzl commented 2 weeks ago

instaling "nitropack": "2.9.7" and adding

"overrides": {
  "nitropack": "2.9.7"
}

to package.json solved my issue for now

pi0 commented 2 weeks ago

✅ Nitro 2.10.1 is released with a (related) fix. Recreating lock file should solve the problem.

aligzl commented 2 weeks ago

Hi Getting this error during deployment

Error: Failed to publish your Function. Got error: Uncaught Error: Missing Cloudflare DB binding (D1) at chunks/nitro/nitro.mjs:1:68337 in createError$1 at chunks/nitro/nitro.mjs:1:489144 in hubDatabase at chunks/nitro/nitro.mjs:1:500109

atinux commented 2 weeks ago

Could you please try with nuxthub/core 0.8.5?

aligzl commented 2 weeks ago

Could you please try with nuxthub/core 0.8.5?

Still same error with fallowing

Error: Failed to publish your Function. Got error: Uncaught Error: Missing Cloudflare DB binding (D1)
  at chunks/nitro/nitro.mjs:1:68337 in createError$1
  at chunks/nitro/nitro.mjs:1:489180 in hubDatabase
  at chunks/nitro/nitro.mjs:1:500032
------------------------------
- Operating System: Darwin
- Node Version:     v21.7.3
- Nuxt Version:     3.14.0
- CLI Version:      3.15.0
- Nitro Version:    2.10.2
- Package Manager:  pnpm@9.9.0
- Builder:          -
- User Config:      default
- Runtime Modules:  @nuxthub/core@0.8.5, @nuxt/eslint@0.6.1, @nuxt/fonts@0.10.2, @nuxt/ui, @vueuse/nuxt@11.2.0, @nuxtjs/i18n@9.0.0, nuxt-security@2.0.0, nuxt-auth-utils@0.5.1
- Build Modules:    -
------------------------------
atinux commented 2 weeks ago

How do you deploy your app @aligzl ?

aligzl commented 2 weeks ago

How do you deploy your app @aligzl ?

trough github actions

aligzl commented 2 weeks ago

Hi @atinux Have you managed to find the issue? as i cant baypass anymore within downgrading.

atinux commented 2 weeks ago

Do you mind inviting me to your project? Did you change anything in the bindings of your project on Cloudflare?

aligzl commented 2 weeks ago

Do you mind inviting me to your project? Did you change anything in the bindings of your project on Cloudflare?

I have invited you to my project and i did not touch anything in the bindings

atinux commented 2 weeks ago

I found the issue @aligzl

You should not call hubDatabase() or hubKV() inside the server/utils/ without wrapping them into a function:

- export const todoDb = new todoService(hubDatabase())
+ export const todoDb = () => new todoService(hubDatabase())

You will need to update todoDb.findOneAndUpdate to todoDb().findOneAndUpdate for example.

This is because the bindings are available only when the request comes in, so inside the event handlers, before this, they are not ready, thus the error happening (I believe Cloudflare try to run the code before deploying it).

aligzl commented 2 weeks ago

I found the issue @aligzl

You should not call hubDatabase() or hubKV() inside the server/utils/ without wrapping them into a function:

- export const todoDb = new todoService(hubDatabase())
+ export const todoDb = () => new todoService(hubDatabase())

You will need to update todoDb.findOneAndUpdate to todoDb().findOneAndUpdate for example.

This is because the bindings are available only when the request comes in, so inside the event handlers, before this, they are not ready, thus the error happening (I believe Cloudflare try to run the code before deploying it).

let me try, But it was working before

Thanks for your kind help

atinux commented 2 weeks ago

It was an issue if it was working before I believe, it was probably related to how the chunks were generated with Nitro.

aligzl commented 2 weeks ago

It works as expected now i think it's a good point to add it to the docs to avoid this kind of mistakes or approaches

Thanks

atinux commented 2 weeks ago

Where do you think would be the best place?