-
Notifications
You must be signed in to change notification settings - Fork 106
Description
Describe the bug
When developing a Nuxt layer using the standard .playground pattern (as generated by the default layer template), NuxtHub writes generated virtual packages to .playground/node_modules/@nuxthub/ because nuxt.options.rootDir resolves to the .playground directory. This makes @nuxthub/db (and likely @nuxthub/blob, @nuxthub/kv, @nuxthub/cache) unreachable from the layer's own code via standard Node module resolution, while the hub:* aliases continue to work because they're resolved through Nuxt's alias system.
Cause
In src/db/setup.ts (L269-270):
const physicalDbDir = join(nuxt.options.rootDir, 'node_modules', '@nuxthub', 'db')
await mkdir(physicalDbDir, { recursive: true })rootDir is the .playground directory when running nuxi dev .playground, so the generated packages are written to:
nuxt-layer/.playground/node_modules/@nuxthub/db
Node module resolution from the layer's server/ directory walks up to nuxt-layer/node_modules/, never reaching .playground/node_modules/. So import { ... } from '@nuxthub/db' fails in the layer code, but import { ... } from 'hub:db' works because Nuxt resolves that alias internally.
The same pattern likely applies to blob, kv, and cache if they use the same rootDir-based path for their generated packages.
Steps to reproduce
- Create a new Nuxt layer using the default template (
npm create nuxt -- --template layer nuxt-layer) - Add
@nuxthub/corewithhub.dbenabled - Add a DB schema in
server/db/schema/ - In the layer's server code, try
import { schema, db } from '@nuxthub/db' - Run
nuxi dev .playground
nuxt-layer/
├── server/db/schema/ # schemas defined here
├── nuxt.config.ts # hub.db config here
├── package.json
└── .playground/
├── nuxt.config.ts # extends: [".."]
└── node_modules/
└── @nuxthub/
├── db/ # generated here (unreachable from layer root)
├── blob/ # likely same issue
├── kv/ # likely same issue
└── cache/ # likely same issue
Expected behavior: @nuxthub/db is importable from the layer code.
Actual: Module not found. Only hub:db resolves correctly.
Additional: Drizzle studio
This also appears to affect Drizzle Studio. When running Drizzle Studio from the layer root, it cannot resolve @nuxthub/db since the generated client only exists inside .playground/node_modules/. The exact resolution strategy Drizzle Studio uses (closest package.json, process.cwd(), etc.) hasn't been confirmed, but the result is the same — the import fails.
Possible fix
Use resolvePackageJSON from pkg-types to find the correct package.json (and therefore the correct node_modules/) to write the generated packages into:
import { resolvePackageJSON } from 'pkg-types'
const packageDir = dirname(await resolvePackageJSON(nuxt.options.rootDir))
const physicalDbDir = join(packageDir, 'node_modules', '@nuxthub', 'db')This would resolve to the layer root's package.json rather than the .playground's, placing the generated @nuxthub/db where Node module resolution can actually find it from the layer code.