Skip to content

Bug: require()-ing a pure-ESM module from CJS crashes the worker #703

@0x5457

Description

@0x5457

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

When a CommonJS module synchronously require()s a pure-ESM module (either
a local .mjs file or a "type": "module" npm package), the user worker
crashes during resolution with:

Error: Expected to resolve main module, got Import instead.
    at loadESMFromCJS (node:module:777)

To Reproduce

Drop a minimal CJS-requires-ESM function into examples/:

examples/commonjs-require-esm/
├── package.json     { "type": "commonjs" }
├── index.js         const esm = require("./message.mjs");
│                    Deno.serve(() => new Response(esm.message));
└── message.mjs      export const message = "cjs require esm ok";

Start the runtime and hit the function:

./scripts/run.sh                              # main worker on :9000
curl http://localhost:9000/commonjs-require-esm

Expected: cjs require esm ok.
Actual: the user worker fails to boot with the error above.

The same happens with any real pure-ESM npm dependency
(e.g. node-fetch@3, chalk@5).

Expected behavior

CJS → ESM require() should succeed, matching Node and Deno's
standalone runtime behavior. The function above should respond with
cjs require esm ok.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions