Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write a package with the option to import subpaths typescript

I wrote a package let's call it commons and another project that imports it.
I do not want to import the whole package but only one subpath of this package.
import { client } from '@commons/clients' instead of import { client } from '@commons'

I have seen typescript packages such as jose that enable such behavior.
E.g. import createRemoteJWKSet from 'jose/jwks/remote'.

After some research, I did see that I can define in package.json this behavior with exports key. Unfortunately, this seems to work only for pure js and not typescript as far as I tested it. Also, I have found that typescript doesn't support exports yet in some thread. How does a package such as jose manage this?

like image 919
Hein Re Avatar asked Oct 20 '25 05:10

Hein Re


1 Answers

This is supported in TypeScript but not by the default module resolution. To allow TypeScript to resolve the module, you need to use either Node16 or NodeNext for your moduleResolution field in tsconfig.json.

like image 146
David R. Myers Avatar answered Oct 21 '25 21:10

David R. Myers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!