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

package.json exports array #3536

Closed
thdxr opened this issue Dec 11, 2023 · 2 comments
Closed

package.json exports array #3536

thdxr opened this issue Dec 11, 2023 · 2 comments

Comments

@thdxr
Copy link

thdxr commented Dec 11, 2023

noticing that when i specify exports like this

  "exports": {
    "./*": [
      "./src/*/index.ts",
      "./src/*.ts"
    ]
  },

esbuild does not seem to find anything matching the second item - switching the order confirms that

any ideas?

@evanw
Copy link
Owner

evanw commented Dec 11, 2023

The path resolution algorithm that esbuild implements for exports intentionally follows node's behavior. This is because node created and specified the algorithm for exports very precisely. Here's what node and esbuild both do for this:

$ cat index.mjs 
import foo from 'pkg/foo'
import bar from 'pkg/bar'
console.log({ foo, bar })

$ cat node_modules/pkg/package.json 
{
  "exports": {
    "./*": [
      "./src/*/index.js",
      "./src/*.js"
    ]
  }
}

$ cat node_modules/pkg/src/foo/index.js 
module.exports = 'foo'

$ cat node_modules/pkg/src/bar.js      
module.exports = 'bar'

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

Error [ERR_MODULE_NOT_FOUND]: Cannot find module './node_modules/pkg/src/bar/index.js' imported from ./index.mjs
    at new NodeError (node:internal/errors:399:5)
    at finalizeResolution (node:internal/modules/esm/resolve:326:11)
    at moduleResolve (node:internal/modules/esm/resolve:945:10)
    at defaultResolve (node:internal/modules/esm/resolve:1153:11)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:838:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)
    at link (node:internal/modules/esm/module_job:76:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Node.js v18.16.1

$ esbuild --bundle index.mjs
✘ [ERROR] Could not resolve "pkg/bar"

    index.mjs:2:16:
      2 │ import bar from 'pkg/bar'
        ╵                 ~~~~~~~~~

  The module "./src/bar/index.js" was not found on the file system:

    node_modules/pkg/package.json:4:6:
      4 │       "./src/*/index.js",
        ╵       ~~~~~~~~~~~~~~~~~~

  You can mark the path "pkg/bar" as external to exclude it from the bundle, which will remove this
  error and leave the unresolved path in the bundle.

1 error

As you can see, esbuild's behavior matches node's behavior, so I consider esbuild's behavior to be correct.

@evanw
Copy link
Owner

evanw commented Dec 12, 2023

Closing as a duplicate of #2974.

@evanw evanw closed this as not planned Won't fix, can't repro, duplicate, stale Dec 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

2 participants