-
Node.js has supported
exportssince v12.7.0 (Jul. 2019)When I asked this question (Dec. 2021), NodeJS had supported the
exportsfield for nearly 2.5 years. It seemed reasonable to assume that Typescript supported it. -
The latest version of TypeScript at that time (4.5) did not support the
exportsfield.This was particularly confusing because the TS 4.5 beta announcement said that it would support
package.jsonexports. -
Typescript 4.7 (Jun. 2022) finally supported
package.jsonexports -
Using
typesVersionsinpackage.jsonis not the solutionSeveral people suggested using
typesVersions– but that’s a completely separate feature which is specific to TypeScript only (read more about it here). Theexportsfield inpackage.jsonis a feature of node and should work with any npm module.
So, if you have a Typescript project and you want to be able to import a package which uses package.json “exports”, you will need to do the following:
- Your TypeScript project must be using TS v4.7 or later
- Your
tsconfigshould be usingmoduleResolutionofnode16ornodenext.- You don’t have to set
moduleResultionif you are usingmodulewith a value ofCommonJS,ES2015,ES6,ES2020, orESNEXT
- You don’t have to set