镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Empty path matching in patterns #49447

Description

@guybedford

@ljharb brought up in the exports patterns PR that it would be worthwhile to ensure the * matching in exports patterns permits empty matches to ensure consistency with all other wildcard schemes.

I agree this conceptual integrity could make sense, and in addition such a feature could be added as a fully backwards compatible change to exports patterns so I would be glad to see this land either way.

One benefit I really liked of not matching the null match is that currently:

{
  "exports": {
    "./": "./sub/"
  }
}

will cause import.meta.resolve('pkg/') to resolve into the sub/ folder. As a result this means import.meta.resolve('pkg/') is not a reliable way to get a package root (and also relates to the package.json resolution discussion previously).

If we now use patterns here:

{
  "exports": {
    "./*": "./sub/*"
  }
}

then as currently written, import.meta.resolve('pkg/') will throw a PACKAGE_PATH_NOT_EXPORTED error because the empty string match is not permitted and thus this "reserves" this space for us to treat it as a special package base resolution case which seems a really useful property for a resolver to have to me in being able to resolve the base of any package this way.

On the other hand I do not have any conceptual arguments against matching the empty string pattern other than that I simply can't think of a single use case in which it is useful. If the argument is purely one of conceptual consistency being important I'm all for that though, but would be concerned about invalidating a perfectly good use case in the name of that.

Activity

  1. hybrist commented on Sep 18, 2020

    @hybrist
    Contributor

    I will throw in that if we make ./sub/* not match pkg/sub/, then we should also make ./sub/ not match pkg/sub/ (assuming that's still possible which I think it is). If we want to encourage people to always use patterns even when prefix would technically also work, it feels weird if there's this one case where an exports can be expressed using prefix but not using pattern.

  2. guybedford commented on Sep 18, 2020

    @guybedford
    ContributorAuthor

    @jkrems we were discussing a multi-major deprecation of path exports. At the current rate of progress on anyone having a clue how to build a standard around import.meta.resolve this is the time frame we are looking for that landing too!

  3. ljharb commented on Sep 18, 2020

    @ljharb
    SponsorMember

    Remember as well that these paths are also used for CJS, and require('pkg/') with CJS's automatic resolution is supremely useful.

  4. guybedford commented on Sep 18, 2020

    @guybedford
    ContributorAuthor

    Ah that's an interesting case. Does that work currently with exports resolution to resolve eg index? If so, I'm not sure the group as a whole made a decision to support that explicitly.

  5. ljharb commented on Sep 19, 2020

    @ljharb
    SponsorMember

    Yes, it does (for CJS), and yes, we definitely did. Only ESM has limited resolution inside a / export.

  6. guybedford commented on Sep 21, 2020

    @guybedford
    ContributorAuthor

    @ljharb I wrote the PR and I didn't know about this case! I disagree in thinking the answer to the fact that require('pkg/') with an "exports": { "./": "./" } entry will resolve pkg/index.js was formerly understood all members of this group let alone explicitly endorsed.

    Here is an example of a case where empty string matches might unintentially expose modules:

    {
      "exports": {
        "./*": "./*/main.js
      }
    }

    Where there exist subfolders like "feature1", "feature2" etc to support import 'pkg/feature1'.

    I do not think it would be obvious that import 'pkg/' would resolve to pkg/main.js in this case. This is because it resolves to .//main.js which resolves in the URL resolver to dedupe the double //.

  7. ljharb commented on Sep 21, 2020

    @ljharb
    SponsorMember

    You can run npx ls-exports in a package if you'd like to see how many CJS entrypoints are exposed in post-ESM node, for a given package/dir. Clearly everyone wasn't aware of it, but we explicitly discussed it in the modules meeting on more than one occasion, to my recollection.

  8. guybedford commented on Sep 21, 2020

    @guybedford
    ContributorAuthor

    @ljharb what is exposed and what is actually used are two different things though. We still have an opportunity to fix minor issues like this that may have slipped through.

  9. added
    moduleIssues and PRs related to the module subsystem.
    and removed
    modules-agendaIssues and PRs to discuss during Modules team meetings.
    on Sep 1, 2023
  10. github-actions commented on Jun 27, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 210 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  11. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jun 27, 2026
  12. github-actions commented on Jul 28, 2026

    @github-actions
    Contributor

    This issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 120 days).
    If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.

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

    moduleIssues and PRs related to the module subsystem.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions