remix-run / remix

Build Better Websites. Create modern, resilient user experiences with web fundamentals.
https://remix.run
MIT License
29.94k stars 2.52k forks source link

Failing Remix-run build on Vercel #9916

Open swamidass opened 2 months ago

swamidass commented 2 months ago

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!

mikkel-arturo commented 2 months ago

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.

mikkel-arturo commented 2 months ago

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.

swamidass commented 2 months ago

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?

MarkusTrb commented 1 month ago

I've been struggling with the same issue during migration. What worked for me is

  1. Making sure to remove all unused imports from these files. Also going through other files that reference them & removing unused imports.
  2. 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.
lazuee commented 1 month ago

vite-plugin-top-level-await is the cause of that problem, try removing that package.

swamidass commented 1 month ago

Thanks! Is there an alternate to this package?

Ehesp commented 1 month ago

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.

jezzzm commented 2 weeks ago

just started seeing this error on 2.13.1 as well

jezzzm commented 2 weeks ago

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:

image