Skip to content

Top-level hub:kv import in secondary-storage.mjs causes build error #33

@onmax

Description

@onmax

Bug Description

When secondaryStorage: true is enabled in nuxt.config, the generated .nuxt/better-auth/secondary-storage.mjs file contains a top-level import:

import { kv } from 'hub:kv'

This causes a build error because the hub:kv virtual module is not available at module evaluation time:

Error: Only URLs with a scheme in: file, data, and node are supported by the default ESM loader. Received protocol 'hub:'

Steps to Reproduce

  1. Install @onmax/nuxt-better-auth and @nuxthub/core
  2. Configure nuxt.config.ts:
export default defineNuxtConfig({
  modules: ['@nuxthub/core', '@onmax/nuxt-better-auth'],
  hub: { kv: true, db: true },
  auth: { secondaryStorage: true }
})
  1. Run npm run dev
  2. Navigate to any page

Expected Behavior

The kv auto-import from NuxtHub should work without errors.

Root Cause

In src/module.ts, the generated code uses a top-level import:

const secondaryStorageCode = secondaryStorageEnabled 
  ? \`import { kv } from 'hub:kv'  // ❌ Top-level import
export function createSecondaryStorage() { ... }\`

Proposed Fix

Since NuxtHub auto-imports kv on the server side, the import statement can be removed entirely:

const secondaryStorageCode = secondaryStorageEnabled 
  ? \`export function createSecondaryStorage() {
  return {
    get: async (key) => kv.get(\\`_auth:\\${key}\\`),  // ✅ Use auto-import
    set: async (key, value, ttl) => kv.set(\\`_auth:\\${key}\\`, value, { ttl }),
    delete: async (key) => kv.del(\\`_auth:\\${key}\\`),
  }
}\`

Alternatively, use the alias that was already resolved:

const hubKvPath = nuxt.options.alias['hub:kv']
const secondaryStorageCode = secondaryStorageEnabled && hubKvPath
  ? \`import { kv } from '${hubKvPath}'  // ✅ Use resolved alias
export function createSecondaryStorage() { ... }\`

Environment

  • @onmax/nuxt-better-auth: 0.0.2-alpha.3
  • @nuxthub/core: 0.10.4
  • Nuxt: 4.2.2

Workaround

Disable secondary storage:

auth: { secondaryStorage: false }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions