Skip to content

Commit 6886a92

Browse files
fix: add createRequire banner to esbuild ESM bundle for Node.js built-in require() support
Agent-Logs-Url: https://github.com/objectstack-ai/framework/sessions/3888d7e6-570c-478b-b969-84043c288a94 Co-authored-by: xuyushun441-sys <255036401+xuyushun441-sys@users.noreply.github.com>
1 parent cfc2227 commit 6886a92

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
the committed wrapper re-exports). This avoids both Vercel's TS
1717
compilation overwriting the bundle (`ERR_MODULE_NOT_FOUND`) and the
1818
"File not found" error from deleting source files during build.
19+
Added `createRequire` banner to the esbuild config so that CJS
20+
dependencies (knex/tarn) can `require()` Node.js built-in modules like
21+
`events` without the "Dynamic require is not supported" error.
1922
Updated rewrites to match: `/api/:path*``/api/[[...route]]`.
2023
- **Studio CORS error on Vercel temporary/preview domains** — Changed
2124
`VITE_SERVER_URL` from hardcoded `https://play.objectstack.ai` to `""`

apps/studio/scripts/bundle-api.mjs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,19 @@ await build({
4545
// Vercel resolves ESM .js files correctly when "type": "module" is set.
4646
// CJS format would conflict with the project's "type": "module" setting,
4747
// causing Node.js to fail parsing require()/module.exports as ESM syntax.
48-
banner: { js: '// Bundled by esbuild — see scripts/bundle-api.mjs' },
48+
//
49+
// The createRequire banner provides a real `require` function in the ESM
50+
// scope. esbuild's __require shim (generated for CJS→ESM conversion)
51+
// checks `typeof require !== "undefined"` and uses it when available,
52+
// which fixes "Dynamic require of <builtin> is not supported" errors
53+
// from CJS dependencies like knex/tarn that require() Node.js built-ins.
54+
banner: {
55+
js: [
56+
'// Bundled by esbuild — see scripts/bundle-api.mjs',
57+
'import { createRequire } from "module";',
58+
'const require = createRequire(import.meta.url);',
59+
].join('\n'),
60+
},
4961
});
5062

5163
console.log('[bundle-api] Bundled server/index.ts → api/_handler.js');

0 commit comments

Comments
 (0)