Dynamic require of "cloudflare:workers" is not supported

I'm trying to create a cloudflare worker that is accessed via services by another worker. I followed the example here https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings/ The worker fails to deploy with the error in the title. I aslo get this warning: ▲ [WARNING] The entrypoint src/gemini.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... Service Worker wrangler toml:
name = "iai-gemini"
main = "src/gemini.ts"
compatibility_date = "2024-08-06"
compatibility_flags = ["nodejs_compat"]
name = "iai-gemini"
main = "src/gemini.ts"
compatibility_date = "2024-08-06"
compatibility_flags = ["nodejs_compat"]
Service worker code (too long for discord) https://pastebin.com/VraghKaZ
Cloudflare Docs
Service bindings | Cloudflare Workers docs
Facilitate Worker-to-Worker communication.
Pastebin
import { GoogleGenerativeAI } from '@google/generative-ai';import {...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
3 Replies
Erisa
Erisa4mo ago
You should use the module syntax instead of service worker syntax
TechParadox
TechParadoxOP4mo ago
@Erisa I am a bit confused by this, so in the service worker I could do something like this?
async function generateCourse(...

export default {
async fetch(request, env, ctx) {
const { subject, assessment, answers, goal } = await request.json;
return await generateCourse(subject, assessment, answers, goal);
},
};
async function generateCourse(...

export default {
async fetch(request, env, ctx) {
const { subject, assessment, answers, goal } = await request.json;
return await generateCourse(subject, assessment, answers, goal);
},
};
In the main worker, how would I call the service worker? Do I no longer need the services binding in wrangler.toml, and instead just directly fetch from "service worker"? And in the export default I just route the request to different functions depending on what function I am trying to call from the "service worker"? What is the best way to call the service worker? Set a binding in the .toml that includes the service workers url (ex. GEMINI = "geminiworker.techparadox.workers.dev" and fetch to env.GEMINI? I got it working, thank you. Seems like the old style was less verbose but I understand the benefits of this style as well
Erisa
Erisa4mo ago
Ah nice, sorry I was busy and missed your message
Want results from more Discord servers?
Add your server