denoland/deno · error · TypeError
Deno.Kv can not be constructed: use Deno.openKv instead
Error message
Deno.Kv can not be constructed: use Deno.openKv instead
What it means
Deno.Kv is a wrapper around a native database resource and cannot be instantiated directly. Its constructor only accepts a private internal symbol (kvSymbol) that Deno.openKv passes internally; any user-side 'new Deno.Kv(...)' fails the symbol check and throws this TypeError. The guard exists so that every Kv instance is bound to a real, opened database handle.
Solutions
- Use `const kv = await Deno.openKv()` (no path opens the default database; `Deno.openKv(":memory:")` gives an isolated in-memory DB).
- Pass an explicit path (`await Deno.openKv("./app.db")`) when you need a persistent on-disk database.
- For tests or dependency injection, wrap or wrap around the instance returned by openKv instead of subclassing Deno.Kv.
- Call `kv.close()` when finished and re-open with openKv rather than trying to re-construct instances.
Example fix
// before
const kv = new Deno.Kv(); // TypeError: use Deno.openKv instead
// after
const kv = await Deno.openKv(); // or await Deno.openKv(":memory:") Defensive patterns
Strategy: validation
Validate before calling
let kv: Deno.Kv;
async function getKv(): Promise<Deno.Kv> {
if (!(kv instanceof Deno.Kv)) kv = await Deno.openKv();
return kv;
} Type guard
const isKv = (v: unknown): v is Deno.Kv => v instanceof Deno.Kv;
Try / catch
try {
const kv = new Deno.Kv();
} catch (e) {
if (e instanceof TypeError && e.message.includes("use Deno.openKv")) {
throw new Error("Deno.Kv must be obtained via `await Deno.openKv()`");
}
throw e;
} Prevention
- Treat Deno.Kv as a capability acquired from Deno.openKv, never a constructor
- Inject the opened kv instance into code under test rather than constructing fakes
- Close handles explicitly with kv.close() and re-open instead of re-constructing
When it happens
Trigger: Calling `new Deno.Kv()` or `new Deno.Kv(rid)`, subclassing (`class MyKv extends Deno.Kv` calling `super()`), or `Reflect.construct(Deno.Kv, [])`. Any construction that does not originate from the internal openKv() factory (ext/kv/01_db.ts:59-62, which does `new Kv(rid, kvSymbol)`) hits the check.
Common situations: Developers coming from `new Map()`-style APIs or other KV libraries assume a public constructor; building test mocks by instantiating Kv directly; editor autocomplete suggesting the constructor signature while the real entry point is the openKv factory.
Related errors
- Value must be a bigint: received
- A chunk may only be either a string or an Uint8Array
- A key and certificate are required for `Deno.listenTls`
- "autoAllocateChunkSize" must be greater than 0
- batchSize must be a positive integer: received
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/387ce6f3f348dca9.
Report an issue: GitHub.
Appendix: source
Thrown at ext/kv/01_db.ts:140
value: Uint8Array;
} | {
kind: "bytes";
value: Uint8Array;
} | {
kind: "u64";
value: bigint;
};
const kvSymbol = Symbol("KvRid");
const commitVersionstampSymbol = Symbol("KvCommitVersionstamp");
class Kv {
#rid: number;
#isClosed: boolean;
constructor(rid: number = undefined, symbol: symbol = undefined) {
if (kvSymbol !== symbol) {
throw new TypeError(
"Deno.Kv can not be constructed: use Deno.openKv instead",
);
}
this.#rid = rid;
this.#isClosed = false;
}
atomic() {
return new AtomicOperation(this.#rid);
}
commitVersionstamp(): symbol {
return commitVersionstampSymbol;
}
async get(key: Deno.KvKey, opts?: { consistency?: Deno.KvConsistencyLevel }) {
const { 0: entries }: [RawKvEntry[]] = await op_kv_snapshot_read(
this.#rid,View on GitHub (pinned to 9ad36f7a2c)