Can't find module "cloudflare:workers" when using `target: 'cloudflare-module'` #4687
Send to my agentWhich project does this relate to?#
Start
Describe the bug#
Create a project with target: 'cloudflare-module' and add a CloudFlare durable object class.
I have created a minimal repo based on the start-basic template.
Your Example Website or App#
https://github.com/astralarya/tanstack-start-cloudflare-durable-objects
Steps to Reproduce the Bug or Issue#
Clone the repo linked above and run:
npm install
npm run dev
Then visit the locally hosted webpage (probably at port 3000).
Expected behavior#
Wrangler should be able to simulate Durable Objects locally during development. Instead, visiting the local webpage gives the following error:
Error: Cannot find module 'cloudflare:workers' imported from '/path/to/repo/tanstack-start-cloudflare-durable-objects/src/utils/durable.ts'
Screenshots or Videos#
No response
Platform#
- Router / Start Version: 1.128.1
- OS: MacOS
- Browser: Chrome
- Browser Version: 138.0.7204.49
- Bundler: vite
- Bundler Version: v6.3.5
Additional context#
The module "cloudflare:workers" is not a regular npm package, but something provided by the Cloudflare runtime in production and Wrangler during development.
I suffer from this issue from time to time. Right now I'm facing it again and the fix was to change this:
import { ShipmentGroupService, type ShipmentGroup } from 'core'to
import type { ShipmentGroup } from 'core' import { ShipmentGroupService, } from 'core'The error only shows up on frontend logs and says
Uncaught TypeError: error loading dynamically imported modulehi! Have you tried specifying the module as external?
In
vite.config.ts:import { tanstackStart } from "@tanstack/react-start/plugin/vite"; import viteReact from "@vitejs/plugin-react"; import { defineConfig } from "vite"; import tsConfigPaths from "vite-tsconfig-paths"; export default defineConfig({ build: { target: "esnext", rollupOptions: { external: ["cloudflare:workers"], }, }, plugins: [ tsConfigPaths({ projects: ["./tsconfig.json"], }), tanstackStart({ target: "cloudflare-module", customViteReactPlugin: true, }), viteReact(), ], });start now supports the cloudflare vite plugin please have a look https://tanstack.com/start/latest/docs/framework/react/hosting#cloudflare-workers--official-partner
if there is still an issue, please open a new github issue
Sign in to join this conversation.
Does anyone have pointers on where to start? I would be happy to help write a fix for this issue