Skip to content

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

@marcosvrs

Description

@marcosvrs

When using @netlify/blobs in a Netlify Background Function that is otherwise fully ESM, the deployed background function fails with the following runtime error:

{
  "errorType": "Error",
  "errorMessage": "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.\nInstead change the require of main.js in /var/task/node_modules/@netlify/blobs/dist/main.cjs to a dynamic import() which is available in all CommonJS modules.",
  "code": "ERR_REQUIRE_ESM",
  "stack": [
    "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.",
    "Instead change the require of main.js in /var/task/node_modules/@netlify/blobs/dist/main.cjs to a dynamic import() which is available in all CommonJS modules.",
    "    at TracingChannel.traceSync (node:diagnostics_channel:322:14)",
    "    at Object.<anonymous> (/var/task/node_modules/@netlify/blobs/dist/main.cjs:42:28)"
  ]
}

Steps to reproduce:

  1. Create a Netlify Background Function with:
    import { getStore } from '@netlify/blobs';
  2. Deploy with the default esbuild bundler.
  3. At runtime, Netlify loads @netlify/blobs/dist/main.cjs, which contains:
    const runtimeUtils = require('@netlify/runtime-utils/dist/main.js')
    …but @netlify/runtime-utils is ESM-only. This triggers ERR_REQUIRE_ESM.

Expected behavior:

  • Background Functions importing @netlify/blobs should work out of the box when using ESM.
  • The CJS build of @netlify/blobs should not require() an ESM-only dependency.

Actual behavior:

  • Deploy ends up using the CJS build of @netlify/blobs, which fails because it require()s an ESM-only file from @netlify/runtime-utils.

Environment:

  • Netlify Functions runtime: Node 22
  • Bundler: default
  • @netlify/build: 35.1.2
  • @netlify/plugin-nextjs: 5.12.1
  • @netlify/blobs version: 10.0.7/10.0.8
  • @netlify/runtime-utils version: 2.1.0
  • Next.js: 14.2.31

Workarounds tested:

  • Adding:

    [functions]
    external_node_modules = ["@netlify/blobs", "@netlify/runtime-utils"]

    to netlify.toml doesn't work.

  • Replacing:

    import { getStore } from '@netlify/blobs';

    with:

    const { getStore } = await import('@netlify/blobs');

    doesn't work.

Request:

  • Could the CJS build of @netlify/blobs be adjusted not to require() ESM-only modules?
  • Alternatively, could @netlify/blobs mark @netlify/runtime-utils as ESM-only and ensure the correct entrypoint is chosen consistently?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions