Typescript 4.7 Supports SubPath Exports and Imports

Updated on

DISCLAIMER: Expressed views on this blog are my own.

You ever wonder how other libraries exposed their library's internal paths with shorthands: @namespace/pkg/subpath? I did when this I had to expose ESM CJS modules so I could use SvelteKit and use some of my libraries in both ESM world and CJS world. Remember, using pkg/subpath/file.js meant you were forced to use a specific path, so you were forced to use CJS paths for broadest support. It's a pain the the rear with Typescript until now.


Modify your package.json as usual: https://nodejs.org/api/packages.html#subpath-exports

Modify tsconfig/jsconfig/etc. with

"moduleResolution": "node16"


Kinda wish their announcement blog post referenced their documentation: https://www.typescriptlang.org/docs/handbook/esm-node.html. It's possible their documentation didn't exist at the time of the blog post though I think it is unlikely since there was a beta of typescript.


You just read "Typescript 4.7 Supports SubPath Exports and Imports". Please share if you liked it!
You can read more recent posts here.