[Bug] TanStack Start 1.142.x breaks production builds with Cloudflare Workers and middleware using platform-specific imports #6185
Send to my agentWhich project does this relate to?#
Start
Describe the bug#
After upgrading from @tanstack/react-start@1.140.5 to @tanstack/react-start@1.142.8, production builds fail when using createMiddleware that imports cloudflare:workers (Cloudflare's platform-specific module for accessing bindings).
The middleware code is only used in .server() blocks, but the imports are being processed during the client build phase, causing Rollup to fail with unresolved imports.
Environment @tanstack/react-start: 1.142.8 (works on 1.140.5) @cloudflare/vite-plugin: 1.19.0 vite: 7.3.0 Platform: Cloudflare Workers
Reproduction
- Create a middleware file that imports cloudflare:workers:
// src/middleware/authentication.ts
import { createMiddleware } from '@tanstack/react-start'
import { getRequest } from '@tanstack/react-start/server'
import { env } from 'cloudflare:workers' // Cloudflare-specific import
export const rateLimitMiddleware = createMiddleware({ type: 'function' })
.server(async ({ next, context }) => {
const { success } = await env.RATE_LIMITER.limit({ key: 'user:123' })
if (!success) throw new Error('Too many requests')
return next()
})
- Use the middleware in a server function:
// src/lib/server-fns/example.ts
import { createServerFn } from "@tanstack/react-start"
import { rateLimitMiddleware } from "@/middleware/authentication"
export const getData = createServerFn()
.middleware([rateLimitMiddleware])
.handler(async () => { /* ... */ })
- Run pnpm run build (or equivalent)
Error Output
error during build:
[vite]: Rollup failed to resolve import "cloudflare:workers" from "src/middleware/authentication.ts".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
If you try to add cloudflare:workers to externals, a secondary error occurs:
node_modules/@tanstack/router-core/dist/esm/ssr/transformStreamWithRouter.js (2:9):
"Readable" is not exported by "__vite-browser-external"
1: import { ReadableStream } from "node:stream/web";
2: import { Readable } from "node:stream";
^
Expected behavior
- Middleware imports should not be analyzed/bundled for the client build
- Server-only code paths should be properly tree-shaken from the client bundle
- Production builds should work the same as 1.140.5
vite.config.ts
import { defineConfig } from 'vite'
import { cloudflare } from '@cloudflare/vite-plugin'
import { tanstackStart } from '@tanstack/react-start/plugin/vite'
// ... other imports
export default defineConfig({
plugins: [
cloudflare({ viteEnvironment: { name: 'ssr' } }),
tanstackStart({ /* ... */ }),
// ... other plugins
],
})
Workaround:
Currently the only workaround is to pin to @tanstack/react-start@1.140.5 and related packages.
Additional context:
This appears related to PR #6160 which restructured server function environment handling. The changes seem to have altered how imports in middleware files are processed during compilation, causing platform-specific imports (like cloudflare:workers) to be included in the client bundle analysis.
Your Example Website or App#
https://github.com/jillesme/linkflare
Steps to Reproduce the Bug or Issue#
- clone repo
- pnpm upgrade tanstack*
- pnpm run build
Expected behavior#
It should build (per current version)
Screenshots or Videos#
No response
Platform#
- Router / Start Version: 1.142.8 (works on 1.140.5)
- OS: macOS
- Browser: N/A (build-time error)
- Browser Version: N/A
- Bundler: Vite
- Bundler Version: 7.3.0
- Node.js: 24.11.1
- @cloudflare/vite-plugin: 1.19.0
Additional context#
No response
this is fixed already but not released unfortunately since there is an issue with publishing. working on it
@schiller-manuel that release did not fix the issue, I updated to 1.142.11 (per https://www.npmjs.com/package/@tanstack/react-router):
pnpm run buildcurrently WORKS.➜ pnpm outdated ┌──────────────────────────────────┬─────────┬──────────┐ │ Package │ Current │ Latest │ ├──────────────────────────────────┼─────────┼──────────┤ │ @testing-library/react (dev) │ 16.3.0 │ 16.3.1 │ ├──────────────────────────────────┼─────────┼──────────┤ │ @tanstack/react-router │ 1.140.5 │ 1.142.11 │ ├──────────────────────────────────┼─────────┼──────────┤ │ @tanstack/react-router-devtools │ 1.140.5 │ 1.142.11 │ ├──────────────────────────────────┼─────────┼──────────┤ │ @tanstack/react-router-ssr-query │ 1.140.5 │ 1.142.11 │ ├──────────────────────────────────┼─────────┼──────────┤ │ @tanstack/react-start │ 1.140.5 │ 1.142.11 │ ├──────────────────────────────────┼─────────┼──────────┤ │ @tanstack/router-plugin │ 1.140.5 │ 1.142.11 │ ├──────────────────────────────────┼─────────┼──────────┤ │ @types/node (dev) │ 22.19.2 │ 25.0.3 │ ├──────────────────────────────────┼─────────┼──────────┤ │ vite-tsconfig-paths │ 5.1.4 │ 6.0.3 │ ├──────────────────────────────────┼─────────┼──────────┤ │ vitest (dev) │ 3.2.4 │ 4.0.16 │ └──────────────────────────────────┴─────────┴──────────┘Then upgrade:
➜ pnpm upgrade @tanstack/react-router @tanstack/react-router-devtools @tanstack/react-router-ssr-query @tanstack/react-start @tanstack/react-router-plugin Packages: +23 -20 +++++++++++++++++++++++-------------------- Progress: resolved 623, reused 456, downloaded 0, added 23, done WARN Issues with peer dependencies found . └─┬ better-auth 1.4.7 ├── ✕ unmet peer drizzle-orm@^0.41.0: found 0.45.1 └── ✕ unmet peer vitest@^4.0.15: found 3.2.4 dependencies: - @tanstack/react-router 1.140.5 + @tanstack/react-router 1.142.11 - @tanstack/react-router-devtools 1.140.5 + @tanstack/react-router-devtools 1.142.11 - @tanstack/react-router-ssr-query 1.140.5 + @tanstack/react-router-ssr-query 1.142.11 - @tanstack/react-start 1.140.5 + @tanstack/react-start 1.142.11Now run pnpm run build again:
➜ pnpm run build > tanstack-start-auth@ build /Users/jilles/Code/linkflare > vite build vite v7.3.0 building client environment for production... transforming (64) node_modules/.pnpm/@tanstack+react-router@1.142.11_react-dom@19.2.3_react@19.2.3__react@19.2 [@tanstack/devtools-vite] Removed devtools code from: /src/routes/__root.tsx ✓ 2013 modules transformed. ✗ Build failed in 791ms error during build: [vite]: Rollup failed to resolve import "cloudflare:workers" from "/Users/jilles/Code/linkflare/src/middleware/authentication.ts". This is most likely unintended because it can break your application at runtime. If you do want to externalize this module explicitly add it to `build.rollupOptions.external` at viteLog (file:///Users/jilles/Code/linkflare/node_modules/.pnpm/vite@7.3.0_@types+node@22.19.2_jiti@2.6.1_lightningcss@1.30.2_tsx@4.21.0/node_modules/vite/dist/node/chunks/config.js:33634:57) at file:///Users/jilles/Code/linkflare/node_modules/.pnpm/vite@7.3.0_@types+node@22.19.2_jiti@2.6.1_lightningcss@1.30.2_tsx@4.21.0/node_modules/vite/dist/node/chunks/config.js:33668:73 at onwarn (file:///Users/jilles/Code/linkflare/node_modules/.pnpm/@vitejs+plugin-react@5.1.2_vite@7.3.0_@types+node@22.19.2_jiti@2.6.1_lightningcss@1.30.2_tsx@4.21.0_/node_modules/@vitejs/plugin-react/dist/index.js:76:7) at file:///Users/jilles/Code/linkflare/node_modules/.pnpm/vite@7.3.0_@types+node@22.19.2_jiti@2.6.1_lightningcss@1.30.2_tsx@4.21.0/node_modules/vite/dist/node/chunks/config.js:33668:28 at onRollupLog (file:///Users/jilles/Code/linkflare/node_modules/.pnpm/vite@7.3.0_@types+node@22.19.2_jiti@2.6.1_lightningcss@1.30.2_tsx@4.21.0/node_modules/vite/dist/node/chunks/config.js:33663:63) at onLog (file:///Users/jilles/Code/linkflare/node_modules/.pnpm/vite@7.3.0_@types+node@22.19.2_jiti@2.6.1_lightningcss@1.30.2_tsx@4.21.0/node_modules/vite/dist/node/chunks/config.js:33466:4) at file:///Users/jilles/Code/linkflare/node_modules/.pnpm/rollup@4.54.0/node_modules/rollup/dist/es/shared/node-entry.js:21032:32 at Object.logger [as onLog] (file:///Users/jilles/Code/linkflare/node_modules/.pnpm/rollup@4.54.0/node_modules/rollup/dist/es/shared/node-entry.js:22919:9) at ModuleLoader.handleInvalidResolvedId (file:///Users/jilles/Code/linkflare/node_modules/.pnpm/rollup@4.54.0/node_modules/rollup/dist/es/shared/node-entry.js:21663:26) at file:///Users/jilles/Code/linkflare/node_modules/.pnpm/rollup@4.54.0/node_modules/rollup/dist/es/shared/node-entry.js:21621:26 ELIFECYCLE Command failed with exit code 1.sorry, the middleware factory pattern is currently broken. working on a fix
schiller-manuel(GitHub profile, opens in a new tab) reopened this
Hi, I am also having the same issue with latest version
1.142.11[plugin vite:resolve] Module "node:async_hooks" has been externalized for browser compatibility, imported by "/Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+start-server-core@1.142.11_crossws@0.4.1_srvx@0.9.8_/node_modules/@tanstack/start-server-core/dist/esm/request-response.js". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details. [plugin vite:resolve] Module "node:stream/web" has been externalized for browser compatibility, imported by "/Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+router-core@1.142.11/node_modules/@tanstack/router-core/dist/esm/ssr/transformStreamWithRouter.js". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details. [plugin vite:resolve] Module "node:stream" has been externalized for browser compatibility, imported by "/Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+router-core@1.142.11/node_modules/@tanstack/router-core/dist/esm/ssr/transformStreamWithRouter.js". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details. [plugin vite:resolve] Module "node:stream" has been externalized for browser compatibility, imported by "/Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+react-router@1.142.11_react-dom@19.2.3_react@19.2.3__react@19.2.3/node_modules/@tanstack/react-router/dist/esm/ssr/renderRouterToStream.js". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details. [plugin vite:resolve] Module "node:async_hooks" has been externalized for browser compatibility, imported by "/Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+start-storage-context@1.142.11/node_modules/@tanstack/start-storage-context/dist/esm/async-local-storage.js". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details. ✓ 2380 modules transformed. ✗ Build failed in 1.03s @repo/plugin-posts:build: error during build: @repo/plugin-posts:build: ../../../node_modules/.pnpm/@tanstack+router-core@1.142.11/node_modules/@tanstack/router-core/dist/esm/ssr/transformStreamWithRouter.js (2:9): "Readable" is not exported by "__vite-browser-external", imported by "../../../node_modules/.pnpm/@tanstack+router-core@1.142.11/node_modules/@tanstack/router-core/dist/esm/ssr/transformStreamWithRouter.js". @repo/plugin-posts:build: file: /Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+router-core@1.142.11/node_modules/@tanstack/router-core/dist/esm/ssr/transformStreamWithRouter.js:2:9 @repo/plugin-posts:build: @repo/plugin-posts:build: 1: import { ReadableStream } from "node:stream/web"; @repo/plugin-posts:build: 2: import { Readable } from "node:stream"; @repo/plugin-posts:build: ^ @repo/plugin-posts:build: 3: import { TSR_SCRIPT_BARRIER_ID } from "./constants.js"; @repo/plugin-posts:build: 4: function transformReadableStreamWithRouter(router, routerStream) { @repo/plugin-posts:build: @repo/plugin-posts:build: at getRollupError (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/rollup@4.53.3/node_modules/rollup/dist/es/shared/parseAst.js:401:41) @repo/plugin-posts:build: at error (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/rollup@4.53.3/node_modules/rollup/dist/es/shared/parseAst.js:397:42) @repo/plugin-posts:build: at Module.error (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/rollup@4.53.3/node_modules/rollup/dist/es/shared/node-entry.js:16956:16) @repo/plugin-posts:build: at Module.traceVariable (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/rollup@4.53.3/node_modules/rollup/dist/es/shared/node-entry.js:17412:29) @repo/plugin-posts:build: at ModuleScope.findVariable (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/rollup@4.53.3/node_modules/rollup/dist/es/shared/node-entry.js:15076:39) @repo/plugin-posts:build: at FunctionScope.findVariable (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/rollup@4.53.3/node_modules/rollup/dist/es/shared/node-entry.js:5649:38) @repo/plugin-posts:build: at FunctionBodyScope.findVariable (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/rollup@4.53.3/node_modules/rollup/dist/es/shared/node-entry.js:5649:38) @repo/plugin-posts:build: at MemberExpression.bind (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/rollup@4.53.3/node_modules/rollup/dist/es/shared/node-entry.js:7363:49) @repo/plugin-posts:build: at CallExpression.bind (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/rollup@4.53.3/node_modules/rollup/dist/es/shared/node-entry.js:2813:23) @repo/plugin-posts:build: at CallExpression.bind (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/rollup@4.53.3/node_modules/rollup/dist/es/shared/node-entry.js:12123:15) @repo/plugin-posts:build: ELIFECYCLE Command failed with exit code 1. @repo/plugin-posts:build: ERROR: command finished with error: command (/Users/jsantosa/Projects/Github/code-flow/packages/plugins/posts) /Users/jsantosa/Library/pnpm/.tools/pnpm/10.26.1/bin/pnpm run build exited (1) @repo/plugin-posts#build: command (/Users/jsantosa/Projects/Github/code-flow/packages/plugins/posts) /Users/jsantosa/Library/pnpm/.tools/pnpm/10.26.1/bin/pnpm run build exited (1)Thanks Best regards
I @schiller-manuel, just upgraded to
1.142.13and still having issues during the build.✗ Build failed in 3.08s app:build: error during build: app:build: [tanstack-start-core::server-fn:client] could not load module /Users/jsantosa/Projects/Github/code-flow/packages/common/react/src/types/actions.ts app:build: file: /Users/jsantosa/Projects/Github/code-flow/packages/integrations/analytics/src/middleware/analytics.ts app:build: at Object.loadModule (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+start-plugin-core@1.142.13_@tanstack+react-router@1.142.13_react-dom@19.2.3_r_df7c46bfb19e878513def7b34e7d12f7/node_modules/@tanstack/start-plugin-core/dist/esm/create-server-fn-plugin/plugin.js:103:27) app:build: at async ServerFnCompiler.getModuleInfo (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+start-plugin-core@1.142.13_@tanstack+react-router@1.142.13_react-dom@19.2.3_r_df7c46bfb19e878513def7b34e7d12f7/node_modules/@tanstack/start-plugin-core/dist/esm/create-server-fn-plugin/compiler.js:698:5) app:build: at async file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+start-plugin-core@1.142.13_@tanstack+react-router@1.142.13_react-dom@19.2.3_r_df7c46bfb19e878513def7b34e7d12f7/node_modules/@tanstack/start-plugin-core/dist/esm/create-server-fn-plugin/compiler.js:535:36 app:build: at async Promise.all (index 1) app:build: at async ServerFnCompiler.findExportInModule (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+start-plugin-core@1.142.13_@tanstack+react-router@1.142.13_react-dom@19.2.3_r_df7c46bfb19e878513def7b34e7d12f7/node_modules/@tanstack/start-plugin-core/dist/esm/create-server-fn-plugin/compiler.js:528:23) app:build: at async Promise.all (index 13) app:build: at async ServerFnCompiler.findExportInModule (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+start-plugin-core@1.142.13_@tanstack+react-router@1.142.13_react-dom@19.2.3_r_df7c46bfb19e878513def7b34e7d12f7/node_modules/@tanstack/start-plugin-core/dist/esm/create-server-fn-plugin/compiler.js:528:23) app:build: at async Promise.all (index 0) app:build: at async ServerFnCompiler.findExportInModule (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+start-plugin-core@1.142.13_@tanstack+react-router@1.142.13_react-dom@19.2.3_r_df7c46bfb19e878513def7b34e7d12f7/node_modules/@tanstack/start-plugin-core/dist/esm/create-server-fn-plugin/compiler.js:528:23) app:build: at async ServerFnCompiler.resolveBindingKind (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+start-plugin-core@1.142.13_@tanstack+react-router@1.142.13_react-dom@19.2.3_r_df7c46bfb19e878513def7b34e7d12f7/node_modules/@tanstack/start-plugin-core/dist/esm/create-server-fn-plugin/compiler.js:577:21) app:build: at async ServerFnCompiler.resolveIdentifierKind (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+start-plugin-core@1.142.13_@tanstack+react-router@1.142.13_react-dom@19.2.3_r_df7c46bfb19e878513def7b34e7d12f7/node_modules/@tanstack/start-plugin-core/dist/esm/create-server-fn-plugin/compiler.js:493:26) app:build: at async ServerFnCompiler.resolveExprKind (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+start-plugin-core@1.142.13_@tanstack+react-router@1.142.13_react-dom@19.2.3_r_df7c46bfb19e878513def7b34e7d12f7/node_modules/@tanstack/start-plugin-core/dist/esm/create-server-fn-plugin/compiler.js:616:26) app:build: at async ServerFnCompiler.resolveCalleeKind (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+start-plugin-core@1.142.13_@tanstack+react-router@1.142.13_react-dom@19.2.3_r_df7c46bfb19e878513def7b34e7d12f7/node_modules/@tanstack/start-plugin-core/dist/esm/create-server-fn-plugin/compiler.js:643:22) app:build: at async ServerFnCompiler.resolveExprKind (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+start-plugin-core@1.142.13_@tanstack+react-router@1.142.13_react-dom@19.2.3_r_df7c46bfb19e878513def7b34e7d12f7/node_modules/@tanstack/start-plugin-core/dist/esm/create-server-fn-plugin/compiler.js:616:26) app:build: at async file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+start-plugin-core@1.142.13_@tanstack+react-router@1.142.13_react-dom@19.2.3_r_df7c46bfb19e878513def7b34e7d12f7/node_modules/@tanstack/start-plugin-core/dist/esm/create-server-fn-plugin/compiler.js:398:15 app:build: at async Promise.all (index 0) app:build: at async ServerFnCompiler.compile (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+start-plugin-core@1.142.13_@tanstack+react-router@1.142.13_react-dom@19.2.3_r_df7c46bfb19e878513def7b34e7d12f7/node_modules/@tanstack/start-plugin-core/dist/esm/create-server-fn-plugin/compiler.js:395:32) app:build: at async Object.handler (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/@tanstack+start-plugin-core@1.142.13_@tanstack+react-router@1.142.13_react-dom@19.2.3_r_df7c46bfb19e878513def7b34e7d12f7/node_modules/@tanstack/start-plugin-core/dist/esm/create-server-fn-plugin/plugin.js:131:26) app:build: at async transform (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/rollup@4.53.3/node_modules/rollup/dist/es/shared/node-entry.js:21163:16) app:build: at async ModuleLoader.addModuleSource (file:///Users/jsantosa/Projects/Github/code-flow/node_modules/.pnpm/rollup@4.53.3/node_modules/rollup/dist/es/shared/node-entry.js:21377:36) app:build: ELIFECYCLE Command failed with exit code 1. app:build: ERROR: command finished with error: command (/Users/jsantosa/Projects/Github/code-flow/apps/app) /Users/jsantosa/Library/pnpm/.tools/pnpm/10.26.1/bin/pnpm run build exited (1) app#build: command (/Users/jsantosa/Projects/Github/code-flow/apps/app) /Users/jsantosa/Library/pnpm/.tools/pnpm/10.26.1/bin/pnpm run build exited (1)The file
actions.tshave the following// biome-ignore lint/suspicious/noExplicitAny: Generic server function type export type Action = (...deps: any[]) => any; export type ActionParams<TFunction extends Action> = Parameters<TFunction>[0]["data"];@jsa4000 can you please create a new issue including a complete reproducer ?
Just following up that it works now.
Sign in to join this conversation.
📝 CodeRabbit Plan Mode#
Generate an implementation plan and prompts that you can use with your favorite coding agent.
Examples
🔗 Similar Issues
Possible Duplicates
Related Issues
🔗 Related PRs
TanStack/router#5215 - fix: various fixes [merged] TanStack/router#5225 - docs: Fix cloudflare config [merged] TanStack/router#5260 - Add basic Cloudflare E2E tests [merged] TanStack/router#5284 - tests(start): update spa mode e2e to use production build [merged] TanStack/router#5847 - docs(solid-start): start-large example [merged]
👤 Suggested Assignees
🧪 Issue enrichment is currently in open beta.
You can configure auto-planning by selecting labels in the issue_enrichment configuration.
To disable automatic issue enrichment, add the following to your
.coderabbit.yaml:💬 Have feedback or questions? Drop into our discord or schedule a call!