Skip to content

miniflare: CommonJS resolution issues (conditional exports and require() from node_modules) #67

Closed

Description

@pi0x

Two module resolution bugs on MiniflareEnvRunner (env-runner 0.3.2, miniflare 4.20260730.0, Node 24) block running Nitro sources unbundled on Cloudflare (nitrojs/nitro#4686). Node.js runners resolve both cases correctly.

1. An ESM import of a package with conditional exports gets the require target.

{ "name": "dual-pkg", "exports": { ".": {
  "node": { "import": "./dist/node.mjs", "require": "./dist/node.cjs" },
  "default": { "import": "./dist/default.mjs", "require": "./dist/default.cjs" }
} } }
// app.mjs (entry)
import * as m from "dual-pkg";
export default { fetch: () => new Response(JSON.stringify(m)) };

Miniflare returns {"default":{"which":"node.cjs"}}, and Node returns {"which":"node.mjs"}. Named imports then fail with does not provide an export named .... In Nitro this loads consola as lib/index.cjs.

2. require() inside a CommonJS file from node_modules resolves from the wrong location.

The CJS module's referrer is the importer's directory plus the file's basename. For example, node_modules/cjs-pkg/dist/index.cjs imported from src/deep/app.mjs gets the referrer src/deep/index.cjs?__cjs. Relative requires still work, but:

  • require("node:process") fails with No such module "node:process". The same builtin works with an ESM import.
  • When a plugin resolveId redirects the required id to a file (e.g. node:process → unenv/dist/runtime/node/process.mjs), that file's relative imports fail with Invalid module specifier "../../../_internal/utils.mjs". The same redirect works from an ESM import.
// node_modules/cjs-pkg/dist/index.cjs
module.exports = { c: typeof require("node:process").nextTick };
// app.mjs (entry)
import cjs from "cjs-pkg";
export default { fetch: () => new Response(cjs.c) };

Reported with AI assistance.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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