nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Custom drivers

Open maximelebreton opened this issue 3 weeks ago • 5 comments

Hello Nitro team!

Expected behavior

I would expect Nitro to support custom driver functions or objects in the storage configuration, not just string identifiers.

Or clarify the documentation that only string identifiers for built-in drivers are supported in nuxt.config.ts: https://unstorage.unjs.io/guide/custom-driver


Describe the bug

When configuring a custom storage driver for Nitro in nuxt.config.ts using defineDriver, Nitro tries to resolve the driver function or object as a package. This results in an error like:

// with driver: upstashCustomDriver()
Cannot find package [object Object]' imported from node_modules/.../nitropack/dist/core/index.mjs
// with driver: upstashCustomDriver
Cannot find package '(opts = {}) => { … }' imported from node_modules/.../nitropack/dist/core/index.mjs

Currently, only string identifiers for built-in drivers (e.g. 'redis', 'memory') work in the config.


Environment

  • Nuxt / Nitro version: 3.x / 2.x
  • Deployment: Vercel serverless
  • Storage: Custom Unstorage driver based on Upstash

Reproduction

// drivers/upstashCustomDriver.ts
import { defineDriver } from 'unstorage';
import { upstashDriver } from 'unstorage/drivers';

export const upstashCustomDriver = defineDriver((opts = {}) => {
  const driver = upstashDriver(opts);
  return {
    ...driver,
    async setItem(key: string, value: any, opts?: { ttl?: number }) {
      const modifiedOpts = opts?.ttl ? { ...opts, ttl: Math.ceil(opts.ttl / 1000) } : opts;
      return driver.setItem!(key, value, modifiedOpts);
    }
  };
});
// nuxt.config.ts
import { upstashCustomDriver } from './drivers/upstashCustomDriver';

export default defineNuxtConfig({
  nitro: {
    storage: {
      upstash: {
        driver: upstashCustomDriver(), // ❌ causes error on Vercel
        //driver: upstashCustomDriver, // ❌ causes error too
        url: process.env.KV_REST_API_URL,
        token: process.env.KV_REST_API_TOKEN
      }
    }
  }
});

maximelebreton avatar Dec 09 '25 17:12 maximelebreton

string identifiers (but this does include passing a path to a custom driver) are required as they become part of your runtime code and we want a hard separation between your config and your code

danielroe avatar Dec 09 '25 17:12 danielroe

@danielroe thanks for your response! But so, could you clarify how to declare a custom driver to be imported as a string in nuxt.config.ts?

I couldn't fin any documentation for the “string” method in Nuxt or Unstorage: https://unstorage.unjs.io/guide/custom-driver

It would be really helpful to have an example for non experts like me :)

maximelebreton avatar Dec 09 '25 18:12 maximelebreton

It is possible to pass the full path to drivers by setting driver to the absolute path of a custom driver (v2 src, v3 src)

Example:

export default defineNuxtConfig({
  nitro: {
    storage: {
      upstash: {
        driver: './drivers/upstashCustomDriver.ts'
        url: process.env.KV_REST_API_URL,
        token: process.env.KV_REST_API_TOKEN
      }
    }
  }

The main limitation in Nitro v2 (current Nuxt), was that runtime code in the driver entry is loaded both in the builder (Node.js) and in the server runtime. It is no longer an issue in Nitro v3 (future Nuxt) as Nitro only imports drivers in the runtime namespace where builder is available, so all runtime features are available.

I think we should improve both docs with some examples.

pi0 avatar Dec 09 '25 18:12 pi0

Thank you @pi0

Just to be sure, could you please confirm that the TypeScript file should look like this?

// drivers/upstashCustomDriver.ts
import { defineDriver } from 'unstorage';
import { upstashDriver } from 'unstorage/drivers';

export default defineDriver((opts = {}) => {
  const driver = upstashDriver(opts);
  return {
    ...driver
    //... custom driver props
  };
});

maximelebreton avatar Dec 09 '25 18:12 maximelebreton

This looks fine!

pi0 avatar Dec 09 '25 19:12 pi0