fix(client-sdk): externalize all Node builtins in Vite lib build
Summary
Issue #25824: On external-fork PRs, server integration tests fail before any
test runs while loading the freshly built twenty-client-sdk. Every shard dies
with:
Error: Module "" has been externalized for browser compatibility. Cannot access ".custom" in client code.
The stack points into packages/twenty-client-sdk/dist/application-*.js. It
reproduces across unrelated contributors and forks, blocking green CI for
changes that have nothing to do with the SDK.
Root cause
twenty-client-sdk is built with Vite in lib mode, which resolves for the
browser by default. When a Node builtin ends up in a chunk and is not marked
external, Vite replaces it with a browser stub that throws
Module … has been externalized for browser compatibility. Cannot access … in client code. the moment the property is accessed. The SDK also runs in Node —
the server imports it during app sync and server integration tests — so that
stub blows up on load.
vite.shared.ts only externalized five builtins (node:fs/promises, node:fs,
node:path, node:os, node:url). Any other builtin pulled into a chunk was
left to be browser-stubbed. Which builtins get bundled depends on dependency
hoisting, so this only surfaces on fresh, uncached installs (external-fork CI);
internal runs that restore the SDK build from cache never exercise the path,
which is why the issue looked contributor-independent and intermittent.
Solution
- In
vite.shared.ts, externalize all Node builtins viabuiltinModules
fromnode:module(both bare andnode:-prefixed forms), instead of the
hardcoded five. This keeps them as real imports the host environment resolves,
so Vite can never emit a browser stub for a builtin regardless of how
dependencies hoist.
Testing
- Fresh
vite build(both the main andvite.metadata.config.tsconfigs)
succeeds and producesdistartifacts byte-identical to the committed ones —
no regression — with no externalization stub present. npx vitest run --root packages/twenty-client-sdk— 104/104 pass.- Note: the original failure only manifests in the external-fork CI
environment's dependency resolution and could not be reproduced locally (a
clean local build was already stub-free), so the definitive check is a fresh
external-fork CI run of the server integration tests.
Fixes #25824