Skip to content

fix(db): create package.json during prepare#797

Merged
atinux merged 5 commits intonuxt-hub:mainfrom
onmax:fix/db-package-json-during-prepare
Jan 29, 2026
Merged

fix(db): create package.json during prepare#797
atinux merged 5 commits intonuxt-hub:mainfrom
onmax:fix/db-package-json-during-prepare

Conversation

@onmax
Copy link
Copy Markdown
Contributor

@onmax onmax commented Jan 23, 2026

Closes #796
Closes #801

Summary

  1. Cyclic TS errors with hub:db after nuxt prepare (db types and path aliases) #796: During nuxt prepare, @nuxthub/db was missing package.json and schema files → TS5090 errors. Fixed by creating stubs in setupDatabaseClient.

  2. TypeScript definitions are lost when running Vitest via @nuxt/test-utils #801: Vitest with @nuxt/test-utils changes buildDir, causing schema copy to overwrite types with stubs. Fixed by skipping copy when VITEST env is set.

  3. DrizzleTypeError with db.query.*: During nuxt build with typescript.typeCheck: 'build', buildDir is node_modules/.cache/nuxt/.nuxt/ where tsdown doesn't generate .d.mts. Fixed by falling back to .nuxt/ for types, merging hooks to prevent race condition, and only writing stubs if files don't exist.

Reproductions

Link Expected
#796 Bug nuxthub-796 ❌ TS5090 error
#796 Fix nuxthub-796-fixed ✅ Typecheck passes
DrizzleTypeError Bug mateusznarowski/drizzle-error db.query.users fails
DrizzleTypeError Fix drizzle-error-797-fix ✅ Build passes

@vercel
Copy link
Copy Markdown

vercel bot commented Jan 23, 2026

@onmax is attempting to deploy a commit to the NuxtLabs Team on Vercel.

A member of the Team first needs to authorize it.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Jan 23, 2026

npm i https://pkg.pr.new/@nuxthub/core@797

commit: 5236b96

@onmax onmax force-pushed the fix/db-package-json-during-prepare branch 2 times, most recently from be858c9 to 06bfa38 Compare January 23, 2026 13:54
@onmax onmax force-pushed the fix/db-package-json-during-prepare branch from 06bfa38 to b2af614 Compare January 23, 2026 14:03
@nogic1008
Copy link
Copy Markdown
Contributor

nogic1008 commented Jan 24, 2026

@onmax
Thank you for your fixes! #801 is resolved on my environment!!
but #792 is not resolved.
Maybe #795 does not resolve #792 because app:templatesGenerated hook does not call during nuxt prepare.

  • prepare: NG(lost definition)
  • typecheck: NG(lost definition)
  • dev: OK(definition generated)
  • test: OK(definition generated)

@onmax
Copy link
Copy Markdown
Contributor Author

onmax commented Jan 24, 2026

Thank you for the report. Just pushed a fix - removed the _prepare guard from the copy block too.

pnpm add @nuxthub/core@https://pkg.pr.new/@nuxthub/core@797

Mind retrying nuxt prepare and nuxt typecheck?

@mateusznarowski
Copy link
Copy Markdown

mateusznarowski commented Jan 26, 2026

I can confirm that after running prepare (with this build) the schema types are generated. However, I encountered an error when running build with typescript.typeCheck="build" set. The error Property 'creators' does not exist on type 'DrizzleTypeError<"Seems like the schema generic is missing - did you forget to add it to your DB type?">'. appears. Running prepare (or dev) again removes this.

This error does not appear with just nuxt typecheck, only with nuxt build along with typeCheck

@onmax
Copy link
Copy Markdown
Contributor Author

onmax commented Jan 26, 2026

I tested with a repro using actual schema types:

// server/api/users.ts
import { db, schema } from '@nuxthub/db'
export default defineEventHandler(async () => {
  const result = await db.select().from(schema.users)
  return result
})
Scenario Main PR #797
Clean install + build ❌ TS5090 ✅ Pass
Remove @nuxthub/db + build ❌ TS5090 ✅ Pass

PR fixes TS5090 path resolution. Couldn't reproduce the DrizzleTypeError specifically @mateusznarowski - could you share your exact schema/usage?

@mateusznarowski
Copy link
Copy Markdown

I didn't describe it accurately, I used db.query. and got this error (as you can see in the attached screenshot). In my case, pnpm install and pnpm build are enough to get the error.

https://github.com/mateusznarowski/drizzle-error

Zrzut ekranu 2026-01-26 o 19 04 23

During build, Nuxt uses node_modules/.cache/nuxt/.nuxt/ as buildDir
where tsdown doesn't generate .d.mts. Fall back to .nuxt/ for types.

Also:
- Merge build+copy into single hook (prevent race condition)
- Only write stubs if files don't exist (prevent overwriting)
@onmax
Copy link
Copy Markdown
Contributor Author

onmax commented Jan 26, 2026

@mateusznarowski Thanks for the repro! I tracked down the issue:

Root cause: During nuxt build with typescript.typeCheck: 'build', Nuxt uses node_modules/.cache/nuxt/.nuxt/ as buildDir. The schema types (.d.mts) weren't being generated there, and the code fell back to writing a stub that broke the Drizzle types.

Fix (commit dc6d7d2):

  1. Merged build+copy hooks into single sequential operation (prevents race condition)
  2. Only write stub files if they don't exist (prevents overwriting real types)
  3. Fall back to .nuxt/ directory when buildDir is in cache

Can you try with the latest from this PR?

pnpm add @nuxthub/core@https://pkg.pr.new/@nuxthub/core@797

The fix reproduction is here: https://stackblitz.com/github/onmax/repros/tree/main/drizzle-error-797-fix?startScript=build

@mateusznarowski
Copy link
Copy Markdown

I checked https://pkg.pr.new/@nuxthub/core@dc6d7d2 and typeCheck is now working correctly! Thank you, and I'm glad I could help.

PS I don't know if you could do anything about it, but I also have an annoying bug in @nuxt/content that I care about, so if you could raise it somewhere, I would be very grateful.

@onmax
Copy link
Copy Markdown
Contributor Author

onmax commented Jan 26, 2026

if you have a repro, I would be more than happy to help you. Please open an issue and mention me :)

@mateusznarowski
Copy link
Copy Markdown

nuxt/content#3700

@onmax Thanks in advance 🙏

@atinux atinux merged commit eb46de5 into nuxt-hub:main Jan 29, 2026
7 of 9 checks passed
@onmax onmax deleted the fix/db-package-json-during-prepare branch January 29, 2026 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeScript definitions are lost when running Vitest via @nuxt/test-utils Cyclic TS errors with hub:db after nuxt prepare (db types and path aliases)

4 participants