Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to resolve subpath exports #978

Closed
IanVS opened this issue Aug 18, 2023 · 2 comments
Closed

Unable to resolve subpath exports #978

IanVS opened this issue Aug 18, 2023 · 2 comments

Comments

@IanVS
Copy link
Contributor

IanVS commented Aug 18, 2023

This one is confusing me. I'm trying to import from msw/browser (using msw@next), but getting this error when I try to build using tsup:

ESM Build start
✘ [ERROR] Could not resolve "msw/browser"

    lib/msw-config.ts:2:46:
      2 │ import { setupWorker, type SetupWorker } from 'msw/browser';
        ╵                                               ~~~~~~~~~~~~~

  The path "./browser" is not exported by package "msw":

    node_modules/msw/package.json:8:13:
      8 │   "exports": {
        ╵              ^

  The file "./lib/browser/index.mjs" is exported at path "./browser":

    node_modules/msw/package.json:19:16:
      19 │       "import": "./lib/browser/index.mjs",
         ╵                 ~~~~~~~~~~~~~~~~~~~~~~~~~

  Import from "msw/browser" to get the file "node_modules/msw/lib/browser/index.mjs":

    lib/msw-config.ts:2:46:
      2 │ import { setupWorker, type SetupWorker } from 'msw/browser';
        │                                               ~~~~~~~~~~~~~
        ╵                                               "msw/browser"

  You can mark the path "msw/browser" as external to exclude it from the bundle, which will remove
  this error.

ESM Build failed
Error: Build failed with 1 error:
lib/msw-config.ts:2:46: ERROR: Could not resolve "msw/browser"

I'm using the latest tsup version, and you can see where this is happening here in this PR: miragejs/mirage-msw#12.

And, here is the package.json of msw, which shows the ./browser export: https://github.com/mswjs/msw/blob/bb02e57568e052206ed8bc55b800cad5fc4c8c78/package.json#L16C8-L22

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@IanVS
Copy link
Contributor Author

IanVS commented Sep 12, 2023

Oh I see, this is coming from esbuild, and matches the behavior of node, because of the "node": null, condition.

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

I would have thought the ESM build would use the import condition, but I guess not since the "node" condition comes first?

@IanVS
Copy link
Contributor Author

IanVS commented Sep 12, 2023

Alright, passing --platform browser solves it. I didn't realize TSUP by default sends platform: node to esbuild, which causes it to consider the node export condition.

@IanVS IanVS closed this as completed Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant