Skip to content

Resolution fails on missing exports array path #44282

@privatenumber

Description

@privatenumber

Version

v18.7.0

Platform

macOS 12.4

Subsystem

No response

What steps will reproduce the bug?

Import a package with an exports array where the first path doesn't exist. For example:

/node_modules/package/package.json:

{
	"exports": ["./missing.js", "./entry.js"]
}

/node_modules/package/entry.js:

console.log('works')

/index.mjs:

import 'package'

Reproduction using Node.js v16.14.2 on StackBlitz:
https://stackblitz.com/edit/node-52xlzc?file=index.js

How often does it reproduce? Is there a required condition?

Always. No.

What is the expected behavior?

For the resolution error to be ignored and to attempt resolution on the next path.

Based on the proposal spec, I believe that's how fallbacks should work.

What do you see instead?

The resolution fails at the first path.

$ node index.mjs
node:internal/errors:477
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/test-node-exports/node_modules/package/missing.js' imported from /test-node-exports/index.mjs
    at new NodeError (node:internal/errors:387:5)
    at finalizeResolution (node:internal/modules/esm/resolve:404:11)
    at moduleResolve (node:internal/modules/esm/resolve:965:10)
    at defaultResolve (node:internal/modules/esm/resolve:1173:11)
    at nextResolve (node:internal/modules/esm/loader:173:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:852:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:439:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
    at link (node:internal/modules/esm/module_job:75:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Additional information

Worth mentioning that the fallback is used when a condition prevents the path from even being attempted:

{
	"exports": [
		{ "some-condition": "./missing.js" },
		"./entry.js"
	]
}

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