Skip to content

Commit a6c3a26

Browse files
authored
fix: add CJS support to @netlify/runtime-utils to fix ERR_REQUIRE_ESM (#510)
@netlify/blobs is dual-format (CJS + ESM) and depends on @netlify/runtime-utils. When the CJS build of @netlify/blobs is used (e.g., in Background Functions), it tries to require() @netlify/runtime-utils, which was ESM-only, causing ERR_REQUIRE_ESM errors at runtime. This change makes @netlify/runtime-utils dual-format so it can be consumed by both CJS and ESM contexts. When @netlify/blobs becomes ESM-only in the future, we can remove CJS support from @netlify/runtime-utils as well. Fixes #437
1 parent 7f3dc71 commit a6c3a26

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

packages/runtime-utils/package.json

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,25 @@
66
"engines": {
77
"node": "^18.14.0 || >=20"
88
},
9-
"main": "./dist/main.js",
10-
"exports": "./dist/main.js",
9+
"main": "./dist/main.cjs",
10+
"module": "./dist/main.js",
1111
"types": "./dist/main.d.ts",
12+
"exports": {
13+
".": {
14+
"require": {
15+
"types": "./dist/main.d.cts",
16+
"default": "./dist/main.cjs"
17+
},
18+
"import": {
19+
"types": "./dist/main.d.ts",
20+
"default": "./dist/main.js"
21+
},
22+
"default": {
23+
"types": "./dist/main.d.ts",
24+
"default": "./dist/main.js"
25+
}
26+
}
27+
},
1228
"files": [
1329
"dist/**/*"
1430
],

packages/runtime-utils/tsup.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ export default defineConfig([
77
clean: true,
88
entry: ['src/main.ts'],
99
outDir: 'dist',
10-
format: ['esm'],
10+
// We build both CJS and ESM because @netlify/blobs is dual-format and depends on this package.
11+
// When @netlify/blobs becomes ESM-only, we can remove CJS here and go ESM-only too.
12+
// See: https://github.com/netlify/primitives/issues/437
13+
format: ['cjs', 'esm'],
1114
dts: true,
1215
splitting: false,
1316
watch: argv.includes('--watch'),

0 commit comments

Comments
 (0)