Worker KV Not Binding

I converted a worker from UI to use github/local to push the code. That sync process is working fine to deploy it. The issue is the KV bindings are not holding. If I manually add the KV in the UI then it works, but on next deployment it wipes out the bindings in the UI. My toml:
name = "auth"
account_id = "123"
workers_dev = true
compatibility_date = "2025-02-10"
compatibility_flags = [ "nodejs_compat" ]

main = "./index.js"

[tail_consumers]
service = "tail"

[observability.logs]
enabled = true

kv_namespaces = [
{ binding = "CURATED_EMAIL", id = "abc", preview_id = "abc" }
]
name = "auth"
account_id = "123"
workers_dev = true
compatibility_date = "2025-02-10"
compatibility_flags = [ "nodejs_compat" ]

main = "./index.js"

[tail_consumers]
service = "tail"

[observability.logs]
enabled = true

kv_namespaces = [
{ binding = "CURATED_EMAIL", id = "abc", preview_id = "abc" }
]
I noticed when testing locally with wrangler dev it doesnt pickup these bindings either. I have setup my worker to handle multiple routes:
//index.js
import { registration } from './registration.js';

export default {
async fetch(request, env) {
const url = new URL(request.url);

if (url.pathname === '/registration') {
return registration(request, env);
}

return new Response('Not Found', { status: 404 });
},
};
//index.js
import { registration } from './registration.js';

export default {
async fetch(request, env) {
const url = new URL(request.url);

if (url.pathname === '/registration') {
return registration(request, env);
}

return new Response('Not Found', { status: 404 });
},
};
And route:
export async function registration(request, env) {
// Convert env object to JSON-safe format
// Changed
const envBindings = {};
for (const key in env) {
envBindings[key] = typeof env[key]; // Show the type of each binding
}

console.log('Available Environment Bindings:', JSON.stringify(envBindings, null, 2));

// Explicitly check if CURATED_EMAIL exists
if (!env.CURATED_EMAIL) {
console.error('ERROR: `CURATED_EMAIL` KV binding is missing in `env`!');
} else {
console.log('CURATED_EMAIL KV binding exists!');
}
export async function registration(request, env) {
// Convert env object to JSON-safe format
// Changed
const envBindings = {};
for (const key in env) {
envBindings[key] = typeof env[key]; // Show the type of each binding
}

console.log('Available Environment Bindings:', JSON.stringify(envBindings, null, 2));

// Explicitly check if CURATED_EMAIL exists
if (!env.CURATED_EMAIL) {
console.error('ERROR: `CURATED_EMAIL` KV binding is missing in `env`!');
} else {
console.log('CURATED_EMAIL KV binding exists!');
}
Everything works fine if I manually add the kv in the UI under worker settings. But I cant get the toml to deploy and actually work. Wipes it out every deployment 😦 Anybody got a clue?
8 Replies
uscjake87
uscjake87OP•3w ago
No description
uscjake87
uscjake87OP•3w ago
Case ID: 01388357
Chaika
Chaika•3w ago
Just toml tables fun. You can use wrangler json configs, or if you ever get confused, try toml to json converters online. They make it very obvious what the issue is, which is having the obs logs table makes and then kv_namespaces below it, makes it part of obs table:
No description
Chaika
Chaika•3w ago
The lazy fix is to move it above any table, or make it into a table itself like
[[kv_namespaces]]
binding = "CURATED_EMAIL"
id= "..."
[[kv_namespaces]]
binding = "CURATED_EMAIL"
id= "..."
.. or use json configs
uscjake87
uscjake87OP•3w ago
ok this is helpful! @Chaika So i made that update so it compiles TOML to JSON properly but it still didn't bind any of the namespaces:
account_id = "123"
compatibility_date = "2024-11-11"
compatibility_flags = [
"nodejs_compat",
"nodejs_compat_v2",
]
main = "./index.js"
name = "auth"
workers_dev = true

[observability.logs]
enabled = true

[[kv_namespaces]]
binding = "CURATED_EMAIL"
id = "abc"
preview_id = "abc"

[[kv_namespaces]]
binding = "CURATED_EMAIL_UNKNOWN"
id = "abc"
preview_id = "abc"

[[kv_namespaces]]
binding = "CURATED_INACTIVE_RETRIES"
id = "abc"
preview_id = "abc"

[[kv_namespaces]]
binding = "CURATED_MULTIPLE_INSTALL"
id = "abc"
preview_id = "abc"

[[kv_namespaces]]
binding = "CURATED_COMPANY"
id = "abc"
preview_id = "abc"

[tail_consumers]
service = "tail"
account_id = "123"
compatibility_date = "2024-11-11"
compatibility_flags = [
"nodejs_compat",
"nodejs_compat_v2",
]
main = "./index.js"
name = "auth"
workers_dev = true

[observability.logs]
enabled = true

[[kv_namespaces]]
binding = "CURATED_EMAIL"
id = "abc"
preview_id = "abc"

[[kv_namespaces]]
binding = "CURATED_EMAIL_UNKNOWN"
id = "abc"
preview_id = "abc"

[[kv_namespaces]]
binding = "CURATED_INACTIVE_RETRIES"
id = "abc"
preview_id = "abc"

[[kv_namespaces]]
binding = "CURATED_MULTIPLE_INSTALL"
id = "abc"
preview_id = "abc"

[[kv_namespaces]]
binding = "CURATED_COMPANY"
id = "abc"
preview_id = "abc"

[tail_consumers]
service = "tail"
Ok made a discovery. So the TOML file still allows a build to go through even if something is wrong/failing and zero log issues. So I started updating the wrangler.jsonc file in addition and finally got some failure logs to discover that I had a wrong KV id number somehow. So whats the point of having a TOML and a jsonc file when they are essentially identical? Should builds only have the jsonc when deploying via github?
Chaika
Chaika•3w ago
Both should have the exact same validation. Toml and jsonc is just different formats for the same config
uscjake87
uscjake87OP•3w ago
Thx. Should only one or the other exist in the project?
Chaika
Chaika•3w ago
You should only use one of them yea

Did you find this page helpful?