cloudflare-docs
cloudflare-docs copied to clipboard
Worker service-binding using RPC example incomplete
Existing documentation URL(s)
https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings/#example--build-your-first-service-binding-using-rpc
What changes are you suggesting?
The example is too light on information (we should link to the reference doc for Service Workers in the example), and the example doesn't work as-is.
If you attempt to deploy a Worker defined as:
import { WorkerEntrypoint } from "cloudflare:workers";
export class WorkerB extends WorkerEntrypoint {
async add(a, b) { return a + b; }
}
Wrangler throws:
▲ [WARNING] The entrypoint src/index.ts has exports like an ES Module, but hasn't defined a default export like a module worker normally would. Building the worker using "service-worker" format...
✘ [ERROR] Unexpected external import of "node:buffer", "node:events", "node:stream", and "node:timers/promises". Imports are not valid in a Service Worker format Worker.
Did you mean to create a Module Worker?
If so, try adding `export default { ... }` in your entry-point
I had to find https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings/rpc/#the-workerentrypoint-class to make it work, where it defines it as:
import { WorkerEntrypoint } from "cloudflare:workers";
export default class extends WorkerEntrypoint {
async add(a, b) { return a + b; }
}
Additional information
No response