Failing Remix-run build on Vercel
Reproduction
The error I get:
[commonjs--resolver] Server-only module referenced by client
'./entry.server.js' imported by 'node_modules/@vercel/remix/dist/edge/index.js'
See https://remix.run/docs/en/main/guides/vite#splitting-up-client-and-server-code
Using this vite.config.js:
import { vitePlugin as remix } from "@remix-run/dev";
import { installGlobals } from "@remix-run/node";
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";
import { vercelPreset } from "@vercel/remix/vite";
import topLevelAwait from "vite-plugin-top-level-await";
import commonjsExternals from 'vite-plugin-commonjs-externals';
import path from "path";
installGlobals();
const externals = ["react-icons"];
export default defineConfig({
resolve: {
alias: {
"~": path.resolve(__dirname, "/app"),
},
},
optimizeDeps: {
exclude: externals,
},
plugins: [
remix({
presets: [vercelPreset()],
future: {
v3_throwAbortReason: true,
}
}),
tsconfigPaths(),
topLevelAwait({
// The export name of top-level await promise for each chunk module
promiseExportName: "__tla",
// The function to generate import names of top-level await promise in each chunk module
promiseImportName: (i) => `__tla_${i}`,
}),
commonjsExternals({ externals }),
],
});
System Info
System:
OS: macOS 14.6.1
CPU: (14) arm64 Apple M3 Max
Memory: 115.98 MB / 36.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.15.1 - ~/.nvm/versions/node/v20.15.1/bin/node
Yarn: 4.0.2 - /opt/homebrew/bin/yarn
npm: 10.7.0 - ~/.nvm/versions/node/v20.15.1/bin/npm
Browsers:
Chrome: 128.0.6613.84
Safari: 17.6
npmPackages:
@remix-run/dev: ^2.11.2 => 2.11.2
@remix-run/eslint-config: ^2.11.2 => 2.11.2
@remix-run/node: ^2.11.2 => 2.11.2
@remix-run/react: ^2.11.2 => 2.11.2
@remix-run/serve: ^2.11.2 => 2.11.2
@remix-run/server-runtime: ^2.11.2 => 2.11.2
@remix-run/testing: ^2.11.2 => 2.11.2
vite: ^5.4.2 => 5.4.2
Used Package Manager
yarn
Expected Behavior
Builds without error.
Actual Behavior
Dev works fine.
remix vite:dev
But building fails.
remix vite:build
The error I get:
[commonjs--resolver] Server-only module referenced by client
'./entry.server.js' imported by 'node_modules/@vercel/remix/dist/edge/index.js'
See https://remix.run/docs/en/main/guides/vite#splitting-up-client-and-server-code
Indeed, edge/index.js does import './entry.server.js' , but edge/index.js isn't client code!
Same issue for me as well, which is a blocker.
I upgraded from 2.10.2 to 2.11.2 and encountered the issue then. I would highly prefer not to downgrade.
Actually, even 2.10.2 is having issues for me.
This works
import {LoaderFunctionArgs} from '@vercel/remix'
import {json} from '@remix-run/react'
This breaks with the error above
import {LoaderFunctionArgs, json} from '@vercel/remix'
Looking in the vercel code, defer and router are just pointing to @remix-run/router anyway, so I've updated my code to refer to it directly and now it is building. Regardless of this workaround, it would be nice to address this.
Thanks for confirming the bug. Perhaps we'll get more help if we can create a mimimal project that reproduces the error? How hard would that be to do?
I've been struggling with the same issue during migration. What worked for me is
- Making sure to remove all unused imports from these files. Also going through other files that reference them & removing unused imports.
- Make sure that in client-side code, all server functions are placed in loader or action. Or when you use helper functions to simplify your loader/action, make sure those helper functions aren't exported.
vite-plugin-top-level-await is the cause of that problem, try removing that package.
Thanks! Is there an alternate to this package?
What's weird is that I just started to get this error, and I have locked versions:
"@remix-run/node": "2.9.2",
"@remix-run/react": "2.9.2",
"@remix-run/serve": "2.9.2",
"@vercel/remix": "2.9.2",
Trying to see what else has changed which would impact this.
just started seeing this error on 2.13.1 as well
look for the way you import types (may depend on your tsconfig setup). i dont know enough about the inner workings, but this fixed for me:
look for the way you import types (may depend on your tsconfig setup). i dont know enough about the inner workings, but this fixed for me:
This worked for me too. This is a major and subtle bug! Please attend
@jezzzm solution worked ! Clearly not documented enough, this can be closed and marked as solution
Thank you for opening this issue, and our apologies we haven't gotten around to it yet!
With the release of React Router v7 we are sun-setting continued development/maintenance on Remix v2. If you have not already upgraded to React Router v7, we recommend you do so. We've tried to make the upgrade process as smooth as possible with our Future Flags. We are now in the process of cleaning up outdated issues and pull requests to improve the overall hygiene of our repositories.
We plan to continue to address 2 types of issues in Remix v2:
- Bugs that pose security concerns
- Bugs that prevent upgrading to React Router v7
If you believe this issue meets one of those criteria, please respond or create a new issue.
For all other issues, ongoing maintenance will be happening in React Router v7, so:
- If this is a bug, please reopen this issue in that repo with a new minimal reproduction against v7
- If this is a feature request, please open a new Proposal Discussion in React Router, and if it gets enough community support it can be considered for implementation
If you have any questions you can always reach out on Discord. Thanks again for providing feedback and helping us make our framework even better!
