🐛 BUG: ReferenceError: KV is not defined
See original GitHub issueWhat version of Wrangler
are you using?
2.0.16
What operating system are you using?
macOS
Describe the Bug
Module Syntax mode is fine (via env). but when I use Service Worker syntax, I faced error is ReferenceError: KV is not defined
index.ts
declare const KV: KVNamespace
addEventListener('fetch', (event: FetchEvent) => {
// @ts-ignore
console.log({ ...global })
console.log(KV)
event.respondWith(new Response("hello"))
})
wrangler.toml
name = "firebase-auth-example"
compatibility_date = "2022-07-05"
workers_dev = false
[vars]
PROJECT_ID = "example-project12345"
kv_namespaces = [
{ binding = "KV", id = "", preview_id = "" }
]
tsconfig = "./tsconfig.json"
Output (when commented out console.log(KV)
line)
[mf:inf] Worker reloaded! (248B)
[mf:inf] Listening on localhost:8787
[mf:inf] - http://localhost:8787
{
global: ServiceWorkerGlobalScope {},
self: ServiceWorkerGlobalScope {},
addEventListener: [Function: addEventListener],
removeEventListener: [Function: removeEventListener],
dispatchEvent: [Function: dispatchEvent],
console: Object [console] {
log: [Function: log],
warn: [Function: warn],
dir: [Function: dir],
time: [Function: time],
timeEnd: [Function: timeEnd],
timeLog: [Function: timeLog],
trace: [Function: trace],
assert: [Function: assert],
clear: [Function: clear],
count: [Function: count],
countReset: [Function: countReset],
group: [Function: group],
groupEnd: [Function: groupEnd],
table: [Function: table],
debug: [Function: debug],
info: [Function: info],
dirxml: [Function: dirxml],
error: [Function: error],
groupCollapsed: [Function: groupCollapsed],
Console: [Function: Console],
profile: [Function: profile],
profileEnd: [Function: profileEnd],
timeStamp: [Function: timeStamp],
context: [Function: context]
},
setTimeout: [Function (anonymous)],
setInterval: [Function (anonymous)],
clearTimeout: [Function: clearTimeout],
clearInterval: [Function: clearInterval],
queueMicrotask: [Function: queueMicrotask],
scheduler: Scheduler {},
atob: [Function: atob],
btoa: [Function: btoa],
Math: Object [Math] {},
crypto: Crypto {},
CryptoKey: [Function: bound CryptoKey],
TextDecoder: [class TextDecoder],
TextEncoder: [class TextEncoder],
fetch: [AsyncFunction: fetch],
Headers: [class Headers],
Request: [class Request extends Body],
Response: [class Response extends Body],
FormData: [class FormData] { name: 'FormData' },
Blob: [class Blob],
File: [class File extends Blob],
URL: [class URL],
URLSearchParams: [class URLSearchParams],
URLPattern: [class URLPattern],
ByteLengthQueuingStrategy: [class ByteLengthQueuingStrategy],
CountQueuingStrategy: [class CountQueuingStrategy],
ReadableByteStreamController: [class ReadableByteStreamController],
ReadableStream: [class ReadableStream],
ReadableStreamBYOBReader: [class ReadableStreamBYOBReader],
ReadableStreamBYOBRequest: [class ReadableStreamBYOBRequest],
ReadableStreamDefaultController: [class ReadableStreamDefaultController],
ReadableStreamDefaultReader: [class ReadableStreamDefaultReader],
TransformStream: [class TransformStream],
TransformStreamDefaultController: [class TransformStreamDefaultController],
WritableStream: [class WritableStream],
WritableStreamDefaultController: [class WritableStreamDefaultController],
WritableStreamDefaultWriter: [class WritableStreamDefaultWriter],
FixedLengthStream: [class FixedLengthStream extends TransformStream],
Event: [class Event] {
NONE: 0,
CAPTURING_PHASE: 1,
AT_TARGET: 2,
BUBBLING_PHASE: 3
},
EventTarget: [class EventTarget] { [Symbol(nodejs.event_target)]: true },
AbortController: [class AbortController],
AbortSignal: [class AbortSignal extends EventTarget],
FetchEvent: [class FetchEvent extends Event],
ScheduledEvent: [class ScheduledEvent extends Event],
DOMException: [class DOMException2 extends Error] {
INDEX_SIZE_ERR: 1,
DOMSTRING_SIZE_ERR: 2,
HIERARCHY_REQUEST_ERR: 3,
WRONG_DOCUMENT_ERR: 4,
INVALID_CHARACTER_ERR: 5,
NO_DATA_ALLOWED_ERR: 6,
NO_MODIFICATION_ALLOWED_ERR: 7,
NOT_FOUND_ERR: 8,
NOT_SUPPORTED_ERR: 9,
INUSE_ATTRIBUTE_ERR: 10,
INVALID_STATE_ERR: 11,
SYNTAX_ERR: 12,
INVALID_MODIFICATION_ERR: 13,
NAMESPACE_ERR: 14,
INVALID_ACCESS_ERR: 15,
VALIDATION_ERR: 16,
TYPE_MISMATCH_ERR: 17,
SECURITY_ERR: 18,
NETWORK_ERR: 19,
ABORT_ERR: 20,
URL_MISMATCH_ERR: 21,
QUOTA_EXCEEDED_ERR: 22,
TIMEOUT_ERR: 23,
INVALID_NODE_TYPE_ERR: 24,
DATA_CLONE_ERR: 25
},
WorkerGlobalScope: [class WorkerGlobalScope extends ThrowingEventTarget],
structuredClone: [Function: structuredClone],
ArrayBuffer: [Function: ArrayBuffer],
Atomics: Object [Atomics] {},
BigInt64Array: [Function: BigInt64Array],
BigUint64Array: [Function: BigUint64Array],
DataView: [Function: DataView],
Date: [Function: Date],
Float32Array: [Function: Float32Array],
Float64Array: [Function: Float64Array],
Int8Array: [Function: Int8Array],
Int16Array: [Function: Int16Array],
Int32Array: [Function: Int32Array],
Map: [Function: Map],
Set: [Function: Set],
SharedArrayBuffer: [Function: SharedArrayBuffer],
Uint8Array: [Function: Uint8Array],
Uint8ClampedArray: [Function: Uint8ClampedArray],
Uint16Array: [Function: Uint16Array],
Uint32Array: [Function: Uint32Array],
WeakMap: [Function: WeakMap],
WeakSet: [Function: WeakSet],
WebAssembly: Object [WebAssembly] {
compile: [Function: compile],
validate: [Function: validate],
instantiate: [Function: instantiate],
compileStreaming: [Function: compileStreaming],
instantiateStreaming: [Function: instantiateStreaming]
},
MINIFLARE: true,
caches: CacheStorage {},
HTMLRewriter: [class HTMLRewriter],
MessageEvent: [class MessageEvent extends Event],
CloseEvent: [class CloseEvent extends Event],
WebSocketPair: [Function: WebSocketPair],
WebSocket: [class WebSocket extends InputGatedEventTarget],
PROJECT_ID: 'example-project12345',
kv_namespaces: [ { binding: 'KV', id: '', preview_id: '' } ],
tsconfig: './tsconfig.json'
}
Run Command
$ wrangler dev index.ts --config=wrangler.toml --local=true
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
KV binding is not defined - Workers - Cloudflare Community
I have my binding set up in wrangler.toml, and set through the ... Uncaught (in promise) ReferenceError: KV_FROM_TEST_WORKER is not defined.
Read more >Cloudflare KV namespace doesn't bind - Stack Overflow
For readers that come to this question via Google and use Wrangler v2 and get this error: Uncaught (in promise) ReferenceError: MY_KV is...
Read more >Serverless Remix Sessions with Cloudflare Pages
ReferenceError: KV is not defined. Attempted to access binding using global in modules. You must use the 2nd `env` parameter passed to exported ......
Read more >How to Use Cloudflare Workers and KV Storage ... - YouTube
Cloudflare workers are like serverless functions that always run at the edge, making them incredibly fast. Not only that, but the tooling ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I was able to reproduce this. The problem is that
kv_namespaces
is being considered as a key under[vars]
. Try replacing that with:For anyone else who may come across this, another alternative is to just move the inline-table version (as was used in the OP for
kv_namespaces
), to be before all other arrays (i.e. the “sections” surrounded in double-square-brackets,[[array_item]]
in the config file).This behaviour actually has nothing to do with Wrangler, and is just how the TOML format works: https://toml.io/en/v1.0.0