nitrojs / nitrojs/nitro

Custom drivers

Open
#3,847 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

discussion documentation nuxt v2 v3
Dominant language
TypeScript
Stars
11.2k
Forks
899
Avg merge
2d 24m
Merged PRs (30d)
40

Description

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
      }
    }
  }
});

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the configuration from nuxt.config.ts with drivers/upstashCustomDriver.ts and trace how Nitro handles the storage driver before the error reported from nitropack/dist/core/index.mjs. Done means custom driver functions or objects work in the Vercel setup, or the documentation clearly states that only string identifiers are supported.

Written by the indexing model from the issue text.

Assessment

Tech stack
nuxt, typescript
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.