I'm using KV namespace defined in cargo.toml
by adding to it the lines
kv_namespaces = [
{ binding = "SITE_DATA", id = "<test-site-id>" }
]
When I try to import the SITE_DATA
variable in my Worker script as shown, and I run wrangler publish
or wrangler build
, I get the error
[tsl] ERROR in /home/x/test/src/handlers/foo.ts(1,29)
TS2306: File '/home/x/test/node_modules/@cloudflare/workers-types/index.d.ts' is not a module.
ts-loader-default_e3b0c44298fc1c14
Why is this error happening, and how do we properly import SITE_DATA
so that we can do things like SITE_DATA.put("data", data)
?
src/handlers/foo.ts
import { KVNamespace } from '@cloudflare/workers-types'
declare const SITE_DATA: KVNamespace
export const Foo = async () => {
const data = {
title: 'Hello World',
}
SITE_DATA.put('posts', JSON.stringify(data))
return new Response("ok")
}
tsconfig.json
{
"compilerOptions": {
"outDir": "./dist",
"module": "commonjs",
"target": "esnext",
"lib": ["esnext"],
"alwaysStrict": true,
"strict": true,
"preserveConstEnums": true,
"moduleResolution": "node",
"sourceMap": true,
"esModuleInterop": true,
"types": [
"@cloudflare/workers-types",
"@types/jest",
"@types/service-worker-mock"
]
},
"include": ["src"],
"exclude": ["node_modules", "dist", "test"]
}