Skip to content

Testing Cloudflare Bindings locally #10389

Closed
@dinkopehar

Description

@dinkopehar

Describe the bug

Hello.

I'm facing a problem which is related to Cloudflare Bindings when using SvelteKit. The documentation on bindings is rather vague.

There is a note at the end stating:

image

Assuming I'm on right track, wrangler can be used to serve SvelteKit in development locally as:

wrangler pages dev -- npm run dev

It will proxy request to SvelteKit. However, to include the binding, I can use (for example KV):

wrangler pages dev --kv=TODO -- npm run dev

I states :

Your worker has access to the following bindings: KV Namespaces: - TODO: TODO

But the problem raises when I try to access platform.env when it running. It states that platform is undefined.

Reproduction

I've created a simple API endpoint.

In a fresh Sveltekit app, in src/routes add folder /kv. In it, create a +server.js with following code:

import { json } from '@sveltejs/kit';

/** @type {import('./$types').RequestHandler} */
export async function POST({ platform }) {
    console.log(platform)  // This is `undefined`.
    console.log(platform.env.KV_NAME.get("example")) // raises error.
    const { a, b } = {a: 1, b: 2}
    return json(a + b);
}

Logs

[mf:inf] POST /kv 500 Internal Server Error (501ms)
[proxy]: 1:36:49 PM [vite] page reload src/routes/kv/+server.js

[proxy]: undefined

✘ [ERROR] [proxy]: TypeError: Cannot read properties of undefined (reading 'env')

      at POST (/home/raziel/Projects/stanovi/src/routes/kv/+server.js:11:33)
      at Module.render_endpoint
  (/home/raziel/Projects/stanovi/node_modules/@sveltejs/kit/src/runtime/server/endpoint.js:49:24)
      at resolve
  (/home/raziel/Projects/stanovi/node_modules/@sveltejs/kit/src/runtime/server/respond.js:409:45)
      at async Module.respond
  (/home/raziel/Projects/stanovi/node_modules/@sveltejs/kit/src/runtime/server/respond.js:279:20)
      at async
  file:///home/raziel/Projects/stanovi/node_modules/@sveltejs/kit/src/exports/vite/dev/index.js:505:22

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish)
    CPU: (4) x64 Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz
    Memory: 5.08 GB / 7.70 GB
    Container: Yes
    Shell: 3.6.1 - /home/raziel/.nix-profile/bin/fish
  Binaries:
    Node: 18.16.1 - ~/.local/share/pnpm_bin/node
    Yarn: 1.22.19 - ~/.nix-profile/bin/yarn
    npm: 9.5.1 - ~/.local/share/pnpm_bin/npm
    pnpm: 8.6.5 - ~/.nix-profile/bin/pnpm
  npmPackages:
    @sveltejs/adapter-auto: ^2.0.0 => 2.1.0 
    @sveltejs/adapter-cloudflare: ^2.3.1 => 2.3.1 
    @sveltejs/kit: ^1.20.4 => 1.22.3 
    svelte: ^4.0.5 => 4.0.5 
    vite: ^4.4.2 => 4.4.4

Severity

serious, but I can work around it

Additional Information

I would like to use bindings when testing locally.

Also, Cloudflare Workers has good documentation on how to test using Jest your worker. It also works with bindings. I'm not sure how to use SvelteKit with this unstable_dev API.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions