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

tsconfig paths and package.json#exports doesn't work together #60460

Open
yusufkandemir opened this issue Nov 8, 2024 · 0 comments
Open

tsconfig paths and package.json#exports doesn't work together #60460

yusufkandemir opened this issue Nov 8, 2024 · 0 comments

Comments

@yusufkandemir
Copy link

πŸ”Ž Search Terms

  • paths aliases exports
  • paths package.json#exports
  • paths node_modules

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried: ~5.4, ~5.5, ~5.6, 5.7.0-beta, 5.7.1-rc, and 5.8.0-dev.20241108

⏯ Playground Link

https://stackblitz.com/edit/ts-paths-and-exports-bug?file=ts-app/tsconfig.json&file=ts-app/1-broken-with-alias.ts&file=ts-app/1-broken-with-alias.ts&view=editor

πŸ’» Code

js-package/package.json:

"exports": {
  "./wrappers": {
    "types": "./lib/app-wrappers.d.ts",
    "import": "./lib/app-wrappers.js"
  }
}

ts-app/0-working-without-alias.ts:

import { defineConfig } from 'js-package/wrappers';

ts-app/1-broken-with-alias.ts:

import { defineConfig } from '#pkg/wrappers';

ts-app/tsconfig.json:

{
  "compilerOptions": {
    // Use an option that respects package.json#exports
    "module": "Preserve", // or NodeNext (both behaves the same in this example)

    "paths": {
      "#pkg": ["node_modules/js-package"]
    }
  }
}

πŸ™ Actual behavior

TypeScript can't resolve #pkg/wrappers and fails. Adding an alias through paths doesn't respect package.json#exports.

Side case: the same happens with just #pkg, so it's not just about subpaths. Unless you provide package.json#types, it doesn't work. So, it doesn't respect something like this either:

"exports": {
  ".": {
    "types": "lib/index.d.ts"
  }
}

πŸ™‚ Expected behavior

I expect tsconfig paths and package.json#exports to work together. TS respects package.json#exports everywhere else fine, so it should also be working fine when using paths too.

Additional information about the issue

A workaround is to define a paths entry that mimicks the exports entry, for every entry point needed. Example:

"paths": {
  "#pkg/wrappers": ["node_modules/js-package/lib/app-wrappers.d.ts"],

  "#pkg": ["node_modules/js-package/lib/index.d.ts"], // or just `node_modules/js-package` and define `package.json#types = lib/index.d.ts`

  "#pkg/other-exports": ["node_modules/js-package/lib/foo.d.ts"],
  "#pkg/yet-another": ["node_modules/js-package/lib/bar/baz.d.ts"],
}
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