Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(erp,mes): inline zustand in SSR bundle to fix prod module-load crash

Brad Barbin95e6584656e2
diff --git a/.ai/lessons.md b/.ai/lessons.md
index cc44d2a5c7..c728587702 100644
--- a/.ai/lessons.md
+++ b/.ai/lessons.md
@@ -268,3 +268,13 @@ Format: `Context → Problem → Rule → Applies to`
**Rule:** For a composite-FK relationship, embed by **target table name** — `.select("*, supplier(id, name)")` (or the explicit constraint `supplier:supplier!purchasingRfqSupplier_supplierId_fkey(...)`), never `alias:fkColumn(...)`. Verify a PostgREST embed against the running REST API (`/rest/v1/<table>?select=...` with the service-role key) — PGRST200 is a schema-cache error returned even on empty tables. And when a loader powers UI state, check `.error`, don't `?? []` a failed query into silent emptiness.
**Applies to:** any supabase-js embed on a join table with a composite `(entityId, companyId)` FK — `purchasingRfqSupplier`, `supplierQuote.supplierId`, and siblings; `apps/erp/app/modules/purchasing/purchasing.service.ts`.
+
+## Dual-major deps of workspace source packages crash the SSR bundle when the shared dep is externalized
+
+**Context:** Merging assembly instructions (#1075) added `@carbon/viewer` (a source-only workspace package) with `@react-three/fiber@8`, whose ESM dist does `import create from 'zustand'` against its own nested zustand v3. The app + `@react-three/drei` use the catalog zustand v5, which removed the default export.
+
+**Problem:** Production (app.carbon.ms) 500'd on every request while builds stayed green. Vite/rolldown inlines deps of linked workspace packages into the SSR bundle but externalizes packages resolvable from the app root **by package name**, merging fiber's v3 default import with v5 named imports into one `import ste,{create,...}from"zustand"` in `build/server/index.js`. Node resolves that to v5 at runtime → `SyntaxError: The requested module 'zustand' does not provide an export named 'default'` at `ModuleJob._instantiate` — before any code runs, so the error never reaches error reporting, dev SSR never reproduces it (per-importer resolution), and Vercel previews show READY (crash is invocation-time). The runtime log dumps a random window of the minified bundle (logtape's timezone formatter), which reads like an Intl/timezone error — red herring.
+
+**Rule:** When a dep of a workspace source package pins a different major of a package the app also depends on, add that package to `ssr.noExternal` in the consuming apps' `vite.config.ts` so each importer keeps its own inlined copy. Verify with a build + `grep -E "from *[\"']zustand" build/server/**` (expect no bare imports) and `node --input-type=module -e "await import('.../build/server/index.js')"` — reaching an env-var error proves linking succeeded. For any all-requests-500 Vercel incident with a minified source dump, read the **last** lines for the real error and check `node:internal/modules/esm/module_job` in the stack before believing anything the dumped source suggests.
+
+**Applies to:** `apps/erp/vite.config.ts`, `apps/mes/vite.config.ts` `ssr.noExternal`; any new dep of `@carbon/viewer` or other source-only workspace packages (`@carbon/form`, `@carbon/onboarding`, ...) that pins an older major of a shared package.
diff --git a/apps/erp/vite.config.ts b/apps/erp/vite.config.ts
index ae0095aa28..3be912f7b8 100644
--- a/apps/erp/vite.config.ts
+++ b/apps/erp/vite.config.ts
@@ -33,6 +33,14 @@ export default defineConfig(({ isSsrBuild, mode }) => {
"react-icons",
"react-phone-number-input",
"tailwind-merge",
+ /**
+ * @react-three/fiber v8 (inlined via @carbon/viewer) default-imports
+ * its nested zustand v3, while the app uses zustand v5 (no default
+ * export). Externalizing zustand merges both into one bare import that
+ * resolves to v5 at runtime and crashes the server at module load.
+ * Bundling it lets each importer keep its own version.
+ */
+ "zustand",
],
},
server: {
diff --git a/apps/mes/vite.config.ts b/apps/mes/vite.config.ts
index e17774c8ea..7cb84a6db8 100644
--- a/apps/mes/vite.config.ts
+++ b/apps/mes/vite.config.ts
@@ -32,6 +32,14 @@ export default defineConfig(({ mode, isSsrBuild }) => {
"react-icons",
"react-phone-number-input",
"tailwind-merge",
+ /**
+ * @react-three/fiber v8 (inlined via @carbon/viewer) default-imports
+ * its nested zustand v3, while the app uses zustand v5 (no default
+ * export). Externalizing zustand merges both into one bare import that
+ * resolves to v5 at runtime and crashes the server at module load.
+ * Bundling it lets each importer keep its own version.
+ */
+ "zustand",
],
},
server: {