Repository navigation
v1.132.2 Rollup failed to resolve import "tanstack-start-injected-head-scripts:v" #5196
Copy link
Copy link
Closed
Description
Which project does this relate to?
Start
Describe the bug
Error during build on version 1.132.2.
error during build:
[vite]: Rollup failed to resolve import "tanstack-start-injected-head-scripts:v" from /node_modules/@tanstack/start-server-core/dist/esm/loadVirtualModule.js".
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://node_modules/vite/dist/node/chunks/dep-D5b0Zz6C.js:33954:57)
at file://node_modules/vite/dist/node/chunks/dep-D5b0Zz6C.js:33990:73
at onwarn (file://node_modules/@vitejs/plugin-react/dist/index.js:55:7)
at file://node_modules/vite/dist/node/chunks/dep-D5b0Zz6C.js:33990:28
at onRollupLog (file://node_modules/vite/dist/node/chunks/dep-D5b0Zz6C.js:33985:63)
at onLog (file://node_modules/vite/dist/node/chunks/dep-D5b0Zz6C.js:33786:4)
at file://node_modules/rollup/dist/es/shared/node-entry.js:20936:32
at Object.logger [as onLog] (file://node_modules/rollup/dist/es/shared/node-entry.js:22822:9)
at ModuleLoader.handleInvalidResolvedId (file://node_modules/rollup/dist/es/shared/node-entry.js:21566:26)
at ModuleLoader.resolveDynamicImport (file://node_modules/rollup/dist/es/shared/node-entry.js:21624:58)[vite] (client) Pre-transform error: Failed to resolve import "tanstack-start-injected-head-scripts:v" from "../../node_modules/@tanstack/start-server-core/dist/esm/loadVirtualModule.js?v=7e60bb72". Does the file exist?
Plugin: vite:import-analysis
File: /home/frixou89/projects/fitable-trpc/node_modules/@tanstack/start-server-core/dist/esm/loadVirtualModule.js?v=7e60bb72:17:27
7 | return await import("tanstack-start-server-fn-manifest:v");
8 | case VIRTUAL_MODULES.injectedHeadScripts:
9 | return await import("tanstack-start-injected-head-scripts:v");
| ^
10 | default:
11 | throw new Error(`Unknown virtual module: ${id}`); (x2)Your Example Website or App
N/A
Steps to Reproduce the Bug or Issue
__root.tsx
function RootComponent() {
const { locale } = Route.useRouteContext();
return (
<RootDocument locale={locale}>
<Outlet />
</RootDocument>
);
}
---
function RootDocument({
children,
locale,
}: Readonly<{ children: ReactNode; locale: string }>) {
return (
<html className="h-full" lang={locale}>
<head>
<HeadContent />
</head>
<body className="h-full">
{children}
<ReactQueryDevtools position="bottom" buttonPosition="bottom-right" />
<Scripts />
</body>
</html>
);
}router.tsx
export function getRouter() {
const router = createTanStackRouter({
routeTree: routeTree,
scrollRestoration: true,
defaultPreloadStaleTime: 0,
context: {
queryClient,
session: null,
},
defaultNotFoundComponent: () => <PageNotFound />,
defaultErrorComponent: (props) => <PageError {...props} />,
Wrap: ({ children }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
),
});
return router;
}Expected behavior
Should work as described in docs and starter app.
If I remove <Scripts /> from the RootDocument component then I get no errors on development but I do on vite build.
Screenshots or Videos
No response
Platform
- Router / Start Version: 1.132.2
- OS: Windows (WSL)
- Browser: Chrome
- Bundler: vite
- Bundler Version: 7.1.6
Additional context
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
MGSimard commentedon Sep 24, 2025
Also dumpstered my static prerendering builds and public folder file references, rolled back to v1.131.50 and it returned to normal.
schiller-manuel commentedon Sep 24, 2025
can you please provide a complete reproducer project for this?
mrhut10 commentedon Sep 24, 2025
yep I'm seeing the same thing on a project i'm attempting to upgrade into that version.
I've parked the upgrade for now
frixou89 commentedon Sep 24, 2025
Managed to reproduce check it here stackblitz
The problem occurs when I import
import { getRequest } from "@tanstack/react-start/server";anywheresupersnager commentedon Sep 24, 2025
When I run vite dev, I see multiple similar errors in the console.
schiller-manuel commentedon Sep 24, 2025
@frixou89 you are importing a server only function inside __root
this will not be compiled away by Start as you dont reference it inside e.g. a server function handler.
moncapitaine commentedon Sep 24, 2025
I am on upgrading my project from a very early tanstack start version to the latest one and get a similar error running vite dev. I already downgraded to 1.132.0 to see if it breaks as well and yes, it does.
Not sure if it makes any difference, but my project is running inside dev-containers, so I need to use:
Here is my console log:
frixou89 commentedon Sep 24, 2025
I do call some server functions from
__root.tsxfor authentication very similar to the example shown here https://tanstack.com/start/latest/docs/framework/react/examples/start-basic-authThis used to work in previous version. Are you saying we can't call server functions from
_root.tsxorbeforeLoadhook anymore?schiller-manuel commentedon Sep 24, 2025
no. i am saying you are importing a server only function from
@tanstack/react-start/serverwhich won't get compiled out for the client build.a server function, i.e. one created using
createServerFnis of course okzenios commentedon Sep 24, 2025
If the server function utilizes a middleware that imports server only types such as
const tokenStorage = new AsyncLocalStorage();
Then the build fails with the messages above.
Prior to rc we used to set/get Context i assume know we have to do it with AsyncLocalStorage
jrnxf commentedon Sep 24, 2025
In my case I was using
serverOnlywhich, though still mentioned in the docs, seems to have been replaced withcreateServerOnlyFn. Changing this fixed the error for memoncapitaine commentedon Sep 25, 2025
Same for me. I used a exported function for usage inside server functions only. These kind of functions need to be createServerOnlyFn to avoid that they are included in the client package. Thx!
supersnager commentedon Sep 25, 2025
@schiller-manuel Thank you for the explanation. In my case it was:
I have another question: Is it a good practice to use getCookie function from this package?
I'm asking because the package's description says:
This package is not meant to be used directly. It is a dependency of @tanstack/react-startIf not, what is the recommended way to get a cookie from the request in SSR?
np-13 commentedon Sep 25, 2025
To reproduce, the following snippet should be added anywhere in the code:
Codesandbox: https://codesandbox.io/p/devbox/dead-code-elimitation-repro-hxnjh2
frixou89 commentedon Sep 25, 2025
Exactly. Is that a bug though or something we should avoid doing and if so, what is the recommended approach when we want to access the request/cookies in a server function?
29 remaining items
schiller-manuel commentedon Oct 11, 2025
@xuxucode why would this be necessary?
xuxucode commentedon Oct 11, 2025
Because in this way, server functions are only exposed in server context, avoid leaking them in client context.
schiller-manuel commentedon Oct 12, 2025
this shouldnt be necessary. when do you think you need this? can you please provide a complete example where this helps?
xuxucode commentedon Oct 12, 2025
I created a repo at https://github.com/xuxucode/tanstack-react-start-ssr based on the
router/examples/react/start-basicexample. All necessary changes are within commit c03abf0schiller-manuel commentedon Oct 12, 2025
@xuxucode you are just doing manually what createIsomorphicFn() gives you. this is not related to the original issue, you were actually calling a server only function in a place that expects isomorphic functions (
loader)xuxucode commentedon Oct 12, 2025
Thanks,
createIsomorphicFnworks.schiller-manuel commentedon Oct 13, 2025
i am closing this now since this is not a bug but was caused by incorrect usage. if anyone encounters a similar issue that is not resolved by following the hints above, then please create a new github issue including a complete minimal reproducer
lino-levan commentedon Oct 13, 2025
Hey @schiller-manuel, I think this issue being open is valuable especially since the error message is so obtuse. We're slowly migrating our app to the tanstack start RC but we're hitting roughly ~10 of these errors throughout our codebase and it's incredibly painful. I would absolutely love to see work towards "hey this file right here is the source of the issues, you can't do that" vs "you messed up, no I'm not going to tell you where". Happy to open another issue to get a better error message here if that's what you'd prefer.
schiller-manuel commentedon Oct 13, 2025
@lino-levan yes please open a new issue for that. we are working on that btw, but tracking this in an issue is good
mike-littlechapter commentedon Nov 18, 2025
I believe this is definitely a bug.
I have file with a single export: a serverFn created using createServerFn({..})
this server function uses getRequestHeader() from '@tanstack/react-start/server' to get the origin header.
it is called in an onClick handler on a button in the app, not even in a loader fn.
using getRequestHeader makes the app un-buildable and the dev server un-runnable.
removing the import of getRequestHeader and not using it fixes everything.
If this isnt a bug, then it would be nice if the "server functions" could be annotated and documented that they are not able to be used within "serverFunctions" (the ones we make using createServerFn)
using a dynamic import within the createServerFn body was not able to fix the issue.
Its think its almost certainly a bug as other server functions within my app are able to use these utilities fine
please re-open this issue.
EDIT:
wrapping the server utility functions in createServerOnlyFn() before calling it within the serverFn resolved the issue
eg
mike-littlechapter commentedon Nov 18, 2025
For anyone landing here please see #5892 for the exact cause of my woes. it may be whats causing your problems if none of the above solutions worked
omergulcicek commentedon Dec 24, 2025
Solution 1: Move Server Functions to Separate Files (Recommended)
After further investigation, the most robust solution is to move your Server Functions into separate files. The "Rollup failed to resolve" error occurs because Vite attempts to bundle server-only utilities (like
getCookies,getRequestUrl, or database clients) into the client-side bundle. When acreateServerFnis defined within the same file as a Client Component, Vite sometimes struggles to tree-shake the server-only dependencies, leading to the virtual module resolution error.The Fix
By moving the Server Function definition to a dedicated
.tsfile, you create a clear boundary between server-side logic and client-side UI.1. Define the Server Function (
login.ts): Keep all server-side logic and heavy imports (like Supabase, DB clients, or Node.js built-ins) here.2. Import in your Component (
LoginPage.tsx): Import the function as a clean reference. Vite will now correctly handle this as a remote call rather than trying to bundle the server code.This approach is architecturally cleaner and prevents the build process from leaking server-only modules into the client bundle. If you are unable to move your functions for some reason, you can still use the Dynamic Import method mentioned below as a fallback.
Solution 2: Use Dynamic Imports (Alternative/Fallback)
The issue is caused by static imports of server-side utilities -such as
getRequestUrl,getCookies, andsetCookie- at the top level of your files. During the build process, Vite attempts to include these in the client bundle, which triggers the virtual module resolution error because these modules are designed to run only in a Node.js/server environment.The Fix
To resolve this, move these imports inside the server function scope using dynamic
imports (await import()). Bu doing so, you prevent Vite from bundling these server-only dependencies into the client-side code.Example
❌ Before (Static Import):
✅ After (Dynamic Import):
Note: Apply this same logic to
getCookiesandsetCookiewithin your handlers to ensure complete compatibility.bilalesi commentedon Dec 27, 2025
this works @omergulcicek, thanks!
omergulcicek commentedon Feb 3, 2026
I’ve encountered the same issue and done a deep dive into why this happens. It usually stems from the server/client boundary being blurred (e.g., importing server-only utilities like Supabase or DB clients into files that end up in the client bundle).
I wrote a detailed guide on how to resolve this by properly separating your server logic and using createServerFn correctly to avoid these virtual module resolution errors.
You can check out the solution here: TanStack Start: Resolving the tanstack-start-injected-head-scripts Error
Hope it helps anyone else stuck on this!