Skip to content

Conversation

@serhalp
Copy link
Member

@serhalp serhalp commented Nov 4, 2025

Problem

Fixes #437

When using @netlify/blobs in a CJS serverless function, the deployed function fails at runtime with ERR_REQUIRE_ESM:

Error [ERR_REQUIRE_ESM]: require() of ES Module /var/task/node_modules/@netlify/runtime-utils/dist/main.js 
from /var/task/node_modules/@netlify/blobs/dist/main.cjs not supported.

Root Cause

  • @netlify/blobs is built in both CJS and ESM formats
  • The CJS build contains require('@netlify/runtime-utils') statements
  • @netlify/runtime-utils was configured to export ESM-only
  • Node.js throws ERR_REQUIRE_ESM when trying to require() an ESM-only module from a CJS context

Solution

Make @netlify/runtime-utils dual-format (CJS + ESM) so it can be consumed by both module systems.

Changes

  1. packages/runtime-utils/tsup.config.ts: Updated to build both cjs and esm formats
  2. packages/runtime-utils/package.json: Added proper conditional exports for both formats

Future Work

When @netlify/blobs becomes ESM-only, we can remove CJS support from @netlify/runtime-utils as well.

Testing

✅ CJS build works:

node -e "const { base64Encode } = require('./packages/runtime-utils/dist/main.cjs'); console.log(base64Encode('test'))"
# Output: dGVzdA==

✅ ESM build still works:

node --input-type=module -e "import { base64Encode } from './packages/runtime-utils/dist/main.js'; console.log(base64Encode('test'))"
# Output: dGVzdA==

@netlify/blobs CJS build loads successfully:

node -e "const blobs = require('./packages/blobs/dist/main.cjs'); console.log(typeof blobs.getStore)"
# Output: function

@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
@serhalp serhalp changed the title fix: add CJS support to @netlify/runtime-utils to fix ERR_REQUIRE_ESM fix: add CJS support to @netlify/runtime-utils to fix ERR_REQUIRE_ESM Nov 4, 2025
@serhalp serhalp marked this pull request as ready for review November 4, 2025 01:38
@serhalp serhalp requested a review from a team as a code owner November 4, 2025 01:38
@serhalp serhalp merged commit a6c3a26 into main Nov 4, 2025
20 of 21 checks passed
@serhalp serhalp deleted the fix-runtime-utils-cjs-support branch November 4, 2025 13:20
@token-generator-app token-generator-app bot mentioned this pull request Nov 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ERR_REQUIRE_ESM when using @netlify/blobs inside Netlify Background Functions (CJS entry requires ESM dependency)

3 participants