Vite error when importing from .server file. "Error: Server-only module referenced by client" #9035
Replies: 7 comments 24 replies
-
#8448 maybe you'll find something useful here |
Beta Was this translation helpful? Give feedback.
-
I am encountering a similar problem. Yet don't know if it's related.
Even though the specified import is only used within the action function. Everything works great when using optimizeDeps: {
entries: [
'**/routes/**/*.{ts,tsx}',
],
}, Anyone got an idea about this? |
Beta Was this translation helpful? Give feedback.
-
For what it's worth: I had a similar problem:
but it turned out, I had imported a function from the I assume this is because remix determines if an import from a .server.js file is actually used in the loader function when deciding to throw this error or not. |
Beta Was this translation helpful? Give feedback.
-
OK now I'm having trouble integrating Trigger.dev because their initialization, which is based on file name routing rather than folder name routing, triggers this error. The automatically generated import { createRemixRoute } from "@trigger.dev/remix";
import { client } from "~/trigger.server";
// Remix will automatically strip files with side effects
// So you need to *export* your Job definitions like this:
export * from "~/jobs/example.server";
export const { action } = createRemixRoute(client); And when I try to build I get the error:
Now, I don't think this should be an error and I think it's on the Remix side, but I'm not sure if anyone is working on this issue or if it's just not going to be fixed but I'd like to be able to integrate Remix with Trigger.dev so I'll try to see if they have any tips as well. |
Beta Was this translation helpful? Give feedback.
-
it also happens when you re-export route relevant modules imported from other file.
`+loader.tsx`
import { db, post } from '~/server/driver/db.server';
export const loader: LoaderFunction = async () => {
const result = await db.select().from(post).all();
return result;
};
...clientLoader, meta, etc `route.tsx`
...
export { loader, clientLoader, meta, shouldRevalidate } from './+loader'; I splitted +loader.file into two, one that exports export { clientLoader, meta, shouldRevalidate } from './+meta';
export { action, headers, loader } from './+server';
export default Route; |
Beta Was this translation helpful? Give feedback.
-
This issue arises, when you have *.server files / folder used outside route modules, because remix does not remove *.server imports. It removes just imports from route modules which are only used within loader and action. If it happens to be a *.server import from the route module file, then everything is fine. But when the *.server import is hidden further down the import-chain, remix isn't going to remove it and so the server-only check will fail. Additionally, pure |
Beta Was this translation helpful? Give feedback.
-
okay so if we're not supposed to use actions outside of routes, how do I do something like... have a signout button in a header that's present everywhere? It seems heavy-handed that Remix should dictate that my header must be part of my dashboard component and can't be a separate file... |
Beta Was this translation helpful? Give feedback.
-
Hello.
I have a file named validation.server.jsx that I use to export a schema validation function.
As the name indicates, it is a .server file with code to run exclusively server-side.
I then use the exported function from the referred file to validate data inside the action of my route.jsx.
Now with Vite, Im getting the error below just by importing the function from the validation.server.jsx file into my app/routes/admin/route.jsx.
import { validateSchema } from "../../lib/validation.server"
By making this import I get the error or any import from a .server file into a route.jsx file i get the error:
It seems that it was dealt with before but im still getting this problem: #8267
Beta Was this translation helpful? Give feedback.
All reactions