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

Missing Cloudflare DB binding (D1) #331

Closed
maxemiliang opened this issue Oct 20, 2024 · 8 comments · Fixed by nitrojs/nitro-cloudflare-dev#49
Closed

Missing Cloudflare DB binding (D1) #331

maxemiliang opened this issue Oct 20, 2024 · 8 comments · Fixed by nitrojs/nitro-cloudflare-dev#49
Labels
bug Something isn't working

Comments

@maxemiliang
Copy link

maxemiliang commented Oct 20, 2024

Describe the bug
When using the onHubReady() hook to run migrations using drizzle in development, the nitro server crashes with the following errors:

ERROR [nitro] [unhandledRejection] Missing Cloudflare DB binding (D1)

There is also a wrangler warning above the error:

 WARN  ▲ [WARNING] Processing .data/hub/wrangler.toml configuration:

    - No environment found in configuration with name "".
      Before using --env= there should be an equivalent environment section in the configuration.

      Consider adding an environment configuration section to the wrangler.toml file:

The generated .data/hub/wrangler.toml file:

[[r2_buckets]]
binding = "BLOB"
bucket_name = "default"

[[kv_namespaces]]
binding = "KV"
id = "kv_default"

[[kv_namespaces]]
binding = "CACHE"
id = "cache_default"

[[d1_databases]]
binding = "DB"
database_name = "default"
database_id = "default"

My package.json:

{
  "name": "nuxt-app",
  "private": true,
  "type": "module",
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev",
    "dev:remote": "nuxt dev --remote",
    "generate": "nuxt generate",
    "preview": "pnpm dlx nuxthub preview",
    "deploy": "pnpm dlx nuxthub deploy",
    "postinstall": "nuxt prepare",
    "lint": "eslint .",
    "db:generate": "drizzle-kit generate",
    "db:studio": "pnpm drizzle-kit studio"
  },
  "dependencies": {
    "@formkit/auto-animate": "^0.8.2",
    "@formkit/core": "^1.6.7",
    "@formkit/nuxt": "^1.6.7",
    "@formkit/vue": "^1.6.7",
    "@formkit/zod": "^1.6.7",
    "@iconify-json/heroicons": "^1.2.0",
    "@nuxt/eslint": "^0.5.7",
    "@nuxt/fonts": "^0.10.0",
    "@nuxt/icon": "^1.5.4",
    "@nuxt/image": "^1.8.1",
    "@nuxthub/core": "^0.7.34",
    "@nuxtjs/tailwindcss": "^6.12.1",
    "@volar/language-server": "2.4.0-alpha.18",
    "@vue/language-server": "2.0.28",
    "@vueuse/core": "^11.1.0",
    "@vueuse/nuxt": "^11.1.0",
    "better-sqlite3": "^11.3.0",
    "drizzle-orm": "^0.33.0",
    "md5": "^2.3.0",
    "nuxt": "^3.13.2",
    "nuxt-auth-utils": "^0.4.2",
    "nuxt-authorization": "^0.3.0",
    "vue": "^3.5.8",
    "vue-router": "^4.4.5",
    "zod": "^3.23.8"
  },
  "devDependencies": {
    "@nuxt/eslint-config": "^0.5.7",
    "@types/md5": "^2.3.5",
    "drizzle-kit": "^0.24.2",
    "eslint": "^9.11.0",
    "eslint-plugin-tailwindcss": "^3.17.4",
    "vue-tsc": "^2.1.6",
    "wrangler": "^3.81.0"
  },
  "engines": {
    "node": ">=20.18.0"
  },
  "packageManager": "pnpm@9.12.0+sha512.4abf725084d7bcbafbd728bfc7bee61f2f791f977fd87542b3579dcb23504d170d46337945e4c66485cd12d588a0c0e570ed9c477e7ccdd8507cf05f3f92eaca"
}

The "faulty" migration code (mostly a copy-paste from the drizzle recipes in the nuxt hub docs):

import { consola } from 'consola'
import { migrate } from 'drizzle-orm/d1/migrator'

export default defineNitroPlugin(async () => {
  if (!import.meta.dev) return

  onHubReady(async () => {
    await migrate(useDrizzle(), { migrationsFolder: 'server/database/migrations' })
      .then(() => {
        consola.success('Database migrations done')
      })
      .catch((err) => {
        consola.error('Database migrations failed', err)
      })
  })
})

Steps to reproduce
Steps I have taken to attempt to resolve the issue:

  1. nuxt cleanup and deleted .data folder
  2. Clean pnpm install
  3. npm run dev

(shamefully-hoist is also set to true in .npmrc)

@atinux
Copy link
Contributor

atinux commented Oct 20, 2024

Damn, seems related to wrangler.

Could you try downgrading or upgrading the Wrangler version?

@maxemiliang
Copy link
Author

maxemiliang commented Oct 21, 2024

So an update to this: it appears that it was not the wrangler version, I tried downgrading to multiple versions, but once I disabled inlineRouteRules, and removed the defineRouteRules from one of my pages (was only using it on one page), it started working again. (I was following this tutorial).

@atinux
Copy link
Contributor

atinux commented Oct 21, 2024

Oh yes! I had the same error when using inlineRouteRules, will investigate thank you!

@atinux
Copy link
Contributor

atinux commented Oct 21, 2024

@maxemiliang I opened a PR on nitro-cloudflare-dev, in the meantime it get released, if you use pnpm, you can update your package.json to:

"resolutions": {
  "nitro-cloudflare-dev": "npm:@atinux/nitro-cloudflare-dev@0.2.1"
}

Then pnpm i, this should do the job until the next @nuxthub/core release that includes this fix 🙏

@atinux atinux added the bug Something isn't working label Oct 21, 2024
@maxemiliang
Copy link
Author

Thank you, that fixed the problem! 🙏 Should I close the issue, or do you close it once the fix goes live? 🙂

Copy link
Contributor

atinux commented Oct 21, 2024

I will close once @nuxthub/core will contain the fix 🙂

@atinux
Copy link
Contributor

atinux commented Oct 22, 2024

Should be good now with https://github.com/nuxt-hub/core/releases/tag/v0.7.35

Make sure to remove the resolutions in your package.json

@maxemiliang
Copy link
Author

Thanks a lot for the help! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants