lukeed/resolve.exports

Resolution depending on the order in `exports` definition

nknapp opened this issue · 2 comments

Is it intended that the resolution of exports depends on the order of properties in the exports definition of the package.json?

  • This is possibly related to mswjs/msw#2092 but there is a reduced test-case a
  • I have extracted the resolve.exports part of the problem into this test-case

tldr;

resolve(packageJson, "./node", conditions, {
    "browser": false,
    "require": false,
    "conditions": [
        "development",
        "module",
        "solid",
        "browser",
        "node"
    ]
} )

throws an error if the package.json contains this snippets:

 "exports": {
                "./node": {
                    "browser": null,
                    "types": "./lib/node/index.d.ts",
                    "require": "./lib/node/index.js",
                    "import": "./lib/node/index.mjs",
                    "default": "./lib/node/index.mjs"
                },
...
}

but does not throw an error if the line browser: null is below the line "default": "./lib/node/index.mjs"

It seems weird to me that order should matter here, because the exports definition is an object and not an array. But I don't know too much about the details of Node's resolution algorihm, so I may be wrong.

Within the "exports" object, key order is significant. During condition matching, earlier entries have higher priority and take precedence over later entries. The general rule is that conditions should be from most specific to least specific in object order.

https://nodejs.org/api/packages.html#conditional-exports

Yup, that's the expected and correct behavior.

I see thanks for the pointer...