Cannot get the kv namespace working with `wrangler dev -e dev`

I’ve the following setup:

src/index.js

export default {
	async fetch(request, env, ctx) {
		const namespace = await env.REDIRECTS.list() 
		console.log(namespace)
		return new Response(namespace.keys)
	},
};

wrangler.toml

name = "using-kv-with-url-path-for-redirect"
account_id = "<account_id>"
main = "src/index.js"
compatibility_date = "2023-10-25"
workers_dev = false

[env.prod]
kv_namespaces = [
    { binding = "REDIRECTS", id = "<redacted>2a"} 
]
routes = [
	{ pattern = "redirect.example.com", custom_domain = true }
]

[env.dev]
kv_namespaces = [
    { binding = "REDIRECTS", id = "<redacted>8a", preview_id = "<redacted>8a" } 
]
routes = [
	{ pattern = "redirect-dev.example.com", custom_domain = true }
]

The one I’m trying to access is correctly bound to the dev worker:

and it does have keys:

But I cannot get it working via npx wrangler dev -e dev:
image

image

2 things to note here:

  • When I’m running locally via npx wrangler dev -e dev, I cannot access kv namespace of any of the key:value pairs in the bounded namespace
  • When I’m deploying the same code via npx wrangler publish -e dev, I can access the key value pairs from the bounded kv namespaces
export default {
	async fetch(request, env, ctx) {
		const namespace = await env.REDIRECTS.list() 
		// console.log(namespace.keys)
		const key_value_1 = await env.REDIRECTS.get('test')
		const key_value_2 = await env.REDIRECTS.get('github')
		console.log(key_value_1)
		console.log(key_value_2)
		return new Response(JSON.stringify(`${key_value_1}, ${key_value_2}`))
	},
};

image

I don’t know why I cannot access kv namespaces using npx wrangler dev -e dev

If someone can help and point out if I’m missing something here.

I am able to solve this with one of the person’s help from official discord channel.

You have to pass --remote flag to get the original worker back to see your kv in local development.

wrangler dev <path-to-script> --remote

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.