vaquilina
vaquilina6d ago

Deno not recognizing default export from npm package 'unstorage' as a function

Hello everyone, I am attempting to set up unstorage with the Deno KV driver, as per https://unstorage.unjs.io/drivers/deno When I import the driver and attempt to call it, the LSP reports that denoKVdriver lacks a call signature, and type-checking also fails:
This expression is not callable.
Type 'typeof import("file:///home/vince/.cache/deno/npm/registry.npmjs.org/unstorage/1.16.0/drivers/deno-kv")' has no call signatures.deno-ts(2349)
This expression is not callable.
Type 'typeof import("file:///home/vince/.cache/deno/npm/registry.npmjs.org/unstorage/1.16.0/drivers/deno-kv")' has no call signatures.deno-ts(2349)
If I invoke denoKVdriver.default(), the LSP no longer reports the error, but type-checking still fails. Relevant d.ts:
import type { Kv } from "@deno/kv";
export interface DenoKvOptions {
base?: string;
path?: string;
openKv?: () => Promise<Deno.Kv | Kv>;
}
declare const _default: (opts: DenoKvOptions) => import("..").Driver<DenoKvOptions, Promise<Deno.Kv | Kv>>;
export default _default;
import type { Kv } from "@deno/kv";
export interface DenoKvOptions {
base?: string;
path?: string;
openKv?: () => Promise<Deno.Kv | Kv>;
}
declare const _default: (opts: DenoKvOptions) => import("..").Driver<DenoKvOptions, Promise<Deno.Kv | Kv>>;
export default _default;
Code:
import { createStorage } from 'unstorage';
import denoKVdriver from 'unstorage/drivers/deno-kv';

export const storage = createStorage({
driver: denoKVdriver({}),
});
import { createStorage } from 'unstorage';
import denoKVdriver from 'unstorage/drivers/deno-kv';

export const storage = createStorage({
driver: denoKVdriver({}),
});
deno.json for workspace member:
{
"tasks": {
"initdb": "deno run init_db.ts"
},
"imports": {
"sqlite": "https://deno.land/x/sqlite/mod.ts"
}
}
{
"tasks": {
"initdb": "deno run init_db.ts"
},
"imports": {
"sqlite": "https://deno.land/x/sqlite/mod.ts"
}
}
deno.json for workspace:
{
"$schema": "https://raw.githubusercontent.com/denoland/deno/main/cli/schemas/config-file.v1.json",
"workspace": ["./authorizer", "./client", "./db", "./api"],
"unstable": ["temporal", "kv"],
"imports": {
"@std/assert": "jsr:@std/assert@^1.0.13",
"unstorage": "npm:unstorage@^1.16.0"
}
}
{
"$schema": "https://raw.githubusercontent.com/denoland/deno/main/cli/schemas/config-file.v1.json",
"workspace": ["./authorizer", "./client", "./db", "./api"],
"unstable": ["temporal", "kv"],
"imports": {
"@std/assert": "jsr:@std/assert@^1.0.13",
"unstorage": "npm:unstorage@^1.16.0"
}
}
Is there a way to work around this?
5 Replies
Mr.Possumz
Mr.Possumz6d ago
It looks like their driver directory doesn't have a general entry file for types and their package.json exports is set up in a way that it may not be able to automatically find it. Try adding a triple slash reference to the top of the file /// <reference types="unstorage/drivers/deno-kv.d.ts"/>
vaquilina
vaquilinaOP6d ago
Thanks for your reply! When adding the triple slash directive to the file, the lsp reports that the export can't be resolved:
NPM package "unstorage@^1.16.0" does not define an export "drivers/deno-kv.d.ts".deno(no-export-npm)
Resolved Dependency

Types: npm​:/unstorage​@^1.16.0/drivers/deno-kv.d.ts
NPM package "unstorage@^1.16.0" does not define an export "drivers/deno-kv.d.ts".deno(no-export-npm)
Resolved Dependency

Types: npm​:/unstorage​@^1.16.0/drivers/deno-kv.d.ts
Mr.Possumz
Mr.Possumz4d ago
y'know ironically, I don't think their package is really configured to be consumed in Deno. It's structured for Node.js It's not great but you could potentially resolve it as such:
import { createStorage } from 'unstorage';
import denoKVdriver from 'unstorage/drivers/deno-kv';
import type denoKvTypes from 'unstorage/drivers/deno-kv';

const driver = denoKVdriver as unknown as typeof denoKvTypes.default;

export const storage = createStorage({
driver: driver({}),
});
import { createStorage } from 'unstorage';
import denoKVdriver from 'unstorage/drivers/deno-kv';
import type denoKvTypes from 'unstorage/drivers/deno-kv';

const driver = denoKVdriver as unknown as typeof denoKvTypes.default;

export const storage = createStorage({
driver: driver({}),
});
vaquilina
vaquilinaOP3d ago
That.. feels bad, but it does work. Thanks again for your help!
vaquilina
vaquilinaOP2d ago
D'oh! Missed this section in the docs. For anyone reading this: https://docs.deno.com/runtime/fundamentals/node/#importing-types
Deno
Node and npm Compatibility
Guide to using Node.js modules and npm packages in Deno. Learn about compatibility features, importing npm packages, and differences between Node.js and Deno environments.

Did you find this page helpful?