T
TanStackโ€ข4mo ago
generous-apricot

Build fails when I use a server function middleware with a server function

The middleware reads the user's data from a session cookie. From what I understand, the error is about me trying to run server-only code on the client side (like reading the encrypted session data), but I created my middleware using the server() method, so I'm confused. Reproduction I forked the basic example and added a commit to it to reproduce the issue. Commit: https://github.com/fulopkovacs/tanstack-start-middleware-issue/commit/60d7283c05ab71353d6014f062242f72fd8be12e#diff-6aa551c33ca5fabc618706416b028e5ce253ca1e09a72d61fe0163b09e2bbcae Repo: https://github.com/fulopkovacs/tanstack-start-middleware-issue โŒ This is the error:
โœ— Build failed in 825ms

[12:16:35 PM] ERROR node_modules/.pnpm/@tanstack+start-server-core@1.120.10/node_modules/@tanstack/start-server-core/dist/esm/transformStreamWithRouter.js (2:9): "Readable" is not exported by "__vite-browser-external", imported by "node_modules/.pnpm/@tanstack+start-server-core@1.120.10/node_modules/@tanstack/start-server-core/dist/esm/transformStreamWithRouter.js".
file: /Users/fulopkovacs/dev-learning/2025/tanstack-start-weirdness/start-basic/node_modules/.pnpm/@tanstack+start-server-core@1.120.10/node_modules/@tanstack/start-server-core/dist/esm/transformStreamWithRouter.js:2:9

1: import { ReadableStream } from "node:stream/web";
2: import { Readable } from "node:stream";
^
3: import { createControlledPromise } from "@tanstack/router-core";
4: function transformReadableStreamWithRouter(router, routerStream) {

[see the full error in the attachment]
โœ— Build failed in 825ms

[12:16:35 PM] ERROR node_modules/.pnpm/@tanstack+start-server-core@1.120.10/node_modules/@tanstack/start-server-core/dist/esm/transformStreamWithRouter.js (2:9): "Readable" is not exported by "__vite-browser-external", imported by "node_modules/.pnpm/@tanstack+start-server-core@1.120.10/node_modules/@tanstack/start-server-core/dist/esm/transformStreamWithRouter.js".
file: /Users/fulopkovacs/dev-learning/2025/tanstack-start-weirdness/start-basic/node_modules/.pnpm/@tanstack+start-server-core@1.120.10/node_modules/@tanstack/start-server-core/dist/esm/transformStreamWithRouter.js:2:9

1: import { ReadableStream } from "node:stream/web";
2: import { Readable } from "node:stream";
^
3: import { createControlledPromise } from "@tanstack/router-core";
4: function transformReadableStreamWithRouter(router, routerStream) {

[see the full error in the attachment]
I started using TanStack Start a week ago and am still a newbie. ๐Ÿ˜… Would be really grateful for some help with this! ๐Ÿ™
GitHub
GitHub - fulopkovacs/tanstack-start-middleware-issue: Repro of an i...
Repro of an issue with server function middlewares. - fulopkovacs/tanstack-start-middleware-issue
5 Replies
generous-apricot
generous-apricotOPโ€ข4mo ago
@Manuel Schiller [continuing the convo I mistakenly opened in the router channel ๐Ÿ˜…] so the middleware is defined in src/utils/authMiddleware.ts , and used in src/utils/posts/. Heyy, sorry to ping you again, but do you have any idea what is the problem here?
foreign-sapphire
foreign-sapphireโ€ข4mo ago
ah sorry i did not look into it more closely the problem you have here is that you export a function useAppSession from the file where the middleware is also exported when start compiles the middleware, it removes the server part of the middleware but it cannot possible remove stuff that is still being used by exported functions so in your case, dont export anything else but middlewares from that file
generous-apricot
generous-apricotOPโ€ข4mo ago
Ohhhh wooow. Thank you very much! It does work with your fix. I'm too tired to understand the reasoning now, but I'll reread it tomorrow morning! ๐Ÿคฃ
foreign-sapphire
foreign-sapphireโ€ข4mo ago
i hope we can provide better error messages for those situations in the future
generous-apricot
generous-apricotOPโ€ข4mo ago
ngl, that'd be awesome ๐Ÿคฃ

Did you find this page helpful?