Cannot file module '@cloudflare:workers'

Just created a new worker with wrangler and tried to import { WorkerEntrypoint } from '@cloudflare:workers' as shown on the docs site here: https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings/#example--build-your-first-service-binding-using-rpc But I get an error from TypeScript that it can't find the module: Cannot find module '@cloudflare:workers' or its corresponding type declarations. typescript (2307) [2, 34] And I also get an error from wrangler when I try to deploy it:
Uncaught Error: Dynamic require of "cloudflare:workers" is not supported
at null.<anonymous> (worker.js:14:11)
at null.<anonymous>
Uncaught Error: Dynamic require of "cloudflare:workers" is not supported
at null.<anonymous> (worker.js:14:11)
at null.<anonymous>
Has anyone run into this issue before? I would appreciate the help, thanks!
Cloudflare Docs
Service bindings - Runtime APIs · Cloudflare Workers docs
Facilitate Worker-to-Worker communication.
18 Replies
Hello, I’m Allie!
What does your Worker look like?
s9tpepper
s9tpepperOP7mo ago
this is the main function:
export class DrCheck extends WorkerEntrypoint {
async check(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const response = await fetch(request)

if (response.status === 200) {
return handle200(request, response, env, ctx)
}

if (response.status > 499) {
return await handle500(request, env)
}

return new Response(response.body, response)
}
}
export class DrCheck extends WorkerEntrypoint {
async check(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const response = await fetch(request)

if (response.status === 200) {
return handle200(request, response, env, ctx)
}

if (response.status > 499) {
return await handle500(request, env)
}

return new Response(response.body, response)
}
}
alien
alien7mo ago
the doc seems to say "cloudflare:workers" not "@cloudflare:workers" on the import, could that be causing it?
s9tpepper
s9tpepperOP7mo ago
:NotLikeThis:
Hello, I’m Allie!
^That, and if that doesn't work, try adding an export default {}; at the bottom
s9tpepper
s9tpepperOP7mo ago
good catch, sorry about that lol - damn, caught by one character
alien
alien7mo ago
:lulujoy:
s9tpepper
s9tpepperOP7mo ago
hrm, nevermind - LSP removed the error and then brought it back :/ the error did change though now its just Unable to resolve path to module 'cloudflare:workers' and wrangler still gives the same error when I try to deploy
Hello, I’m Allie!
Try adding export default {}; at the bottom
s9tpepper
s9tpepperOP7mo ago
so wrangler lets me deploy if I add this:
export default {
fetch() {
// eslint-disable-next-line
console.log('unimplemented')
},
}
export default {
fetch() {
// eslint-disable-next-line
console.log('unimplemented')
},
}
The error about being unable to resolve cloudflare:workers is still there though - not sure why
alien
alien7mo ago
did u npm i?
s9tpepper
s9tpepperOP7mo ago
yea I dont think the solution is usable though, the test interface throws an error trying to use that worker now as a binding Could not serialize object of type \"Fetcher\". This type does not support serialization. - I think I'm going to have to retreat on trying to use the worker as RPC
kian
kian7mo ago
I wouldn’t expect that you’re able to pass stuff like env or ctx Even requests I guess, since they have request.fetcher
s9tpepper
s9tpepperOP7mo ago
maybe that's where the serialization errors are coming from, I'll refactor those objects out and retry
MBrimmer
MBrimmer5mo ago
@s9tpepper Were you able to resolve "The error about being unable to resolve cloudflare:workers is still there though - not sure why"? If so how?
yarn add cloudflare:workers
➤ YN0027: cloudflare:workers@unknown can't be resolved to a satisfying range
➤ YN0035: Package not found
➤ YN0035: Response Code: 404 (Not Found)
➤ YN0035: Request Method: GET
➤ YN0035: Request URL: https://registry.yarnpkg.com/cloudflare:workers

➤ Errors happened when preparing the environment required to run this command.
yarn add cloudflare:workers
➤ YN0027: cloudflare:workers@unknown can't be resolved to a satisfying range
➤ YN0035: Package not found
➤ YN0035: Response Code: 404 (Not Found)
➤ YN0035: Request Method: GET
➤ YN0035: Request URL: https://registry.yarnpkg.com/cloudflare:workers

➤ Errors happened when preparing the environment required to run this command.
I think I solved it by upgrading "@Cloudflare/workers-types" to the latest version.
Chinoman10
Chinoman105mo ago
My @cloudflare/workers-types are at the latest version, but I can't seem to be able to import { DurableObject } from "cloudflare:workers"; :shrugg:
Brod
Brod3mo ago
did you resolve this @Chinoman10? I might have this same issue:
No description
Hello, I’m Allie!
Please do not reopen old threads. If you are having issues, please open a new thread instead
Want results from more Discord servers?
Add your server