It would help if you could share your full worker.
Are you using Service Workers, or Module Workers?
With Service Workers (which use global scope & addEventListener), bindings are on the global scope, i.e
addEventListener("fetch", async (event) => {
let value = await KV.get("to-do:123");
.....
With Module Workers, bindings are on env
export default {
async fetch(request, env, ctx) {
let value = await env.KV.get("to-do:123");
.....
Make sure you are using the right format/passing env through.
I believe D1 still only supports Module Workers as well.
You can find a get started guide here:
ps. You might have better luck/easier responses with Workers questions/issues in the Cloudflare Developer Discord, if you need more help, feel free to pop in there, there’s a #d1-open-alpha channel.
Thanks for attaching your full worker.
I was just using KV as an example of how you could access a binding on both environments.
env doesn’t exist in service workers, only in module workers.
D1 only works in Module Workers. D1 is a bit hacky right now and only works from wrangler as well, as it needs a shim, and the shim only works with Module Workers, hence the restriction. Perhaps when it goes to General Release, Service workers will be supported.
Module Workers aren’t too different from Service Workers, if you need D1, you can migrate your worker:
(There are other advantages to Module Workers as well, faster start, safer, etc)