Skip to content

Document: under the ownership models a dual package's import loads its require build #838

Open

Description

@danfry1

Under experimental.ownershipModel ('resolver' or 'resolution'), an ES import of a package resolves through the require export condition. Metro resolves it through import.

"exports": {
  ".": { "import": "./build/index.mjs", "require": "./build/index.js", "default": "./build/index.js" }
}

import * as Badge from "pkg" in a test file loads build/index.js under both ownership models, with or without the hot runtime. Without the flag it loads build/index.mjs. Metro 0.87 picks the condition per dependency (metro-resolver matchSubpathFromExportsLike: context.isESMImport === true ? "import" : "require"), so an app bundle gets build/index.mjs.

A package whose ES and CommonJS builds differ therefore runs a different build under the ownership models than in the app. The fixture tests-native/fixtures/rn-esbuild-reexport-lib shows it: import.meta.resolve("rn-esbuild-reexport-lib") from a test under tests-native/vitest.ownership.config.mts gives build/index.js.

Expected: an ES import resolves through import, and a require through require, as Metro decides it.

Activity

  1. added this to the 0.16.0 milestone on Nov 14, 2026
  2. added
    bug
    area:ownership
    priority:medium
    on Nov 14, 2026
  3. danfry1 commented on Nov 14, 2026

    @danfry1
    OwnerAuthor

    Decision: keep the one-instance rule. This becomes a documented known difference, not a resolver change.

    The ownership models give every package one owner and one file, so a test's import and a library's require of the same package return the same object. packages/vitest-native/docs/ownership-model.md ("require wins") records this as the intended trade-off, and src/native/shared-resolver.mjs implements it: one condition set, the CommonJS one Node applies in the workers.

    Matching Metro per request would bring back the case the models exist to remove. Metro picks import or require for each dependency, so an app that reaches a dual package both ways bundles both builds, and module-level state (React contexts, registries, instanceof checks) splits between them. In a test, that shows up as a context reading its default value or a second registry. That is worse than running the CommonJS build of a package whose two builds are normally the same source compiled twice.

    Narrower rules considered and not taken:

    • The owner's realm picks the condition (import for packages Vite owns, require for packages Node owns). This keeps one file, but a require of a Vite-owned dual package would then get the ES build's namespace instead of module.exports, which Metro never gives a require. It trades one difference from Metro for another.
    • import for everything. This relies on require(esm) for every Node-owned package, and React Native packages and their Jest support expect CommonJS semantics there.
    • Per-request conditions with a dedupe check. Two files cannot be one module instance, so this goes back to the dual-instance case.

    Follow-up:

    • Document it as a known difference: in the guide's package entry points section (website/guide/how-it-works.md) and in the fidelity matrix. Say that under the ownership models a package with separate import and require builds runs its require build in tests, that an app bundle may use the import build, and that resolve.alias pins a specific file where the two builds behave differently.
    • Revisit if a real package turns up whose ES and CommonJS builds behave differently in a way a test can observe. That evidence would justify a per-package opt-in rather than a change to the default.

    This issue is now a documentation item. The docs change will close it.

  4. added
    documentation
    and removed
    bug
    on Nov 14, 2026
  5. changed the title [-]Ownership models resolve an ES import through the require condition, unlike Metro[/-] [+]Document: under the ownership models a dual package's import loads its require build[/+] on Nov 14, 2026
  6. danfry1 commented on Nov 15, 2026

    @danfry1
    OwnerAuthor

    This stays docs-only. Under the ownership models, require wins for a dual package, and only one instance loads; that is a deliberate design choice. The change is to document it: the "Package entry points" section of website/guide/how-it-works.md, and a row in the fidelity matrix, should say that an ES import of a dual package loads its require build under the ownership models, unlike Metro.

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

    area:ownershipdocumentationpriority:medium

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions