Errors from conditional exports after upgraded to Node 13.13

  • Version: Node.js 13.13
  • Platform: 19.4.0 Darwin Kernel Version 19.4.0
  • Subsystem: ES modules

What steps will reproduce the bug?

npm install @babel/compat-data
node -e "require('@babel/compat-data/corejs3-shipped-proposals')"

What is the expected behavior?

It should load the module as expected in Node.js 13.12

What do you see instead?

Error: Cannot find module '@babel/compat-data/corejs3-shipped-proposals'
Require stack:
- /tmp/[eval]
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:952:15)
    at Function.resolve (internal/modules/cjs/helpers.js:78:19)
    at [eval]:1:9
    at Script.runInThisContext (vm.js:131:20)
    at Object.runInThisContext (vm.js:297:38)
    at Object.<anonymous> ([eval]-wrapper:10:26)
    at Module._compile (internal/modules/cjs/loader.js:1123:30)
    at evalScript (internal/process/execution.js:94:25)
    at internal/main/eval_string.js:23:3 {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/tmp/[eval]' ]
}

Additional information

Caught by the Babel CI: https://travis-ci.com/github/babel/babel/jobs/319348116#L360

The regression was likely introduced in #32351.

The file structure of @babel/compat-data can be simplified w.l.o.g as

package.json
corejs3-shipped-proposals.js
data/corejs3-shipped-proposals.json

and the package.json:

"exports": {
  "./corejs3-shipped-proposals": "./data/corejs3-shipped-proposals.json"
}

The corejs3-shipped-proposals.js is a proxy which delegates to "./data/corejs3-shipped-proposals.json" for Node.js < 13.3 support.

module.exports = require("./data/corejs3-shipped-proposals.json");