Using service binding to call a worker from a pages function locally

I have a Cloudflare Page with a bunch of Functions. From one of those Functions, I want to call a Worker using Service Binding, as described here…

I also want to be able to test it locally. In that documentation, that is described here…… it says to add --service=<BINDING_NAME>=<WORKER_NAME> to the run command (so, for example, wrangler pages dev <OUTPUT_DIR> --service=SERVICE=my-worker).

However, if I try to do that, I get an error (from Wrangler) that “service” is an unknown argument.

It also says I will need to have the worker (“my-worker” in the example) “running in wrangler pages dev --local” - I’m not sure what that means.

Has anyone successfully got this working locally? Thanks!

1 Like

Same here… I’m trying to develop locally with wrangler pages dev to call a separate worker API service binding, but “–service” seems to be an unsupported parameter - at least in wrangler 3.1.0.

Documentation that describes how to set up service bindings in cloudflare pages that mentions the --service argument: Bindings · Cloudflare Pages docs

(Current) Source code for wrangler pages dev that doesn’t have the --service argument: Commands · Cloudflare Workers docs

The --service argument is also not listed in wrangler’s documentation: Commands · Cloudflare Workers docs

Strange that KV, Env Vars, R2, D1, and DO are all supported, but not service bindings.

For now, my workaround is to just use a normal fetch as if it was some external service, not an internal service binding.

I’ve logged an issue here: 🐛 BUG: wrangler pages dev doesn't support service bindings or the `--service` argument · Issue #3436 · cloudflare/workers-sdk · GitHub